To solve this problem, some observations have to be made first.
Let's first see two relatively easy observations.
- To maximize the probability that we can complete the circuit, at each gas station, we will add all the available gas to the tank.
- If sum_{i = 1, 2, ..., k}gas[i] < sum_{i = 1, 2, ..., k}cost[i], then we cannot reach k if we start from i.
Make sure you convince yourself of these two observations. They will serve as the foundation for the following trickier observations.
- Starting from i, if the first unreachable position is k, then we cannot reach position k if we start from any position between i and k.
Let's do a quick proof. We use proof by contradiction. Suppose we can reach k if we start from the position j which is between i and k. Now we can reach k from j. Moreover, we can reach j from i (since k is the first unreachable position starting from i). Putting these together, we will first reach j from i, and then reach k from j. This contradicts the assumption that k is not reachable from i.
With this observation, each time we find the first point that the we cannot reach from the current starting position. We know that we do not need to check all the points between them and simply skip to the next point of the first unreachable point.
Now comes the last observation, which is the key to give a O(n) solution.
- If the sum of gas is not less than the sum of cost, there must be a solution.
Well, the proof of this observation is not as easy as the above one. If there are only two gas stations, this observation is easy. When there are more than two gas stations, you may need to merge some of them to a single one and reduce the effective number of gas stations. You mar refer to this link for a proof.
Using these two observations, the idea to solve this problem is as follows.
Maintain a variable tank for the net gas in the tank and a variable total to accumulate the net difference between the sum of gas and the sum of cost. Then we initialize a variable start to be 0 for the final starting position. Each time we find an unreachable point, we update start to be the point after the current unreachable point. After we traverse all the points, we will have the final net difference between the sum of gas and the sum of cost. If it is not less than 0, we know there must be a solution (according to the last observation) and return start. Otherwise, return -1.
The code is as follows.
1 class Solution { 2 public: 3 int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { 4 int start = 0, total = 0, tank = 0; 5 for (int i = 0; i < (int)gas.size(); i++) { 6 tank += gas[i] - cost[i]; 7 if (tank < 0) { 8 start = i + 1; 9 total += tank; 10 tank = 0; 11 } 12 } 13 return total + tank >= 0 ? start : -1; 14 } 15 };
Well, let's do more with the last observation. In fact, if the sum of gas is not less than the sum of cost, there exists a position with the minimum net gas (sum_{i=0, 1, ..., k}gas[i] - sum_{i = 0, 1, ..., k}cost[i] is minimized) and the starting point is simply the point after it.
Using this idea, we will have a more succinct code.
1 class Solution { 2 public: 3 int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { 4 int net = 0, start = 0, min_net = 0; 5 for (int i = 0; i < (int)gas.size(); i++) { 6 net += gas[i] - cost[i]; 7 if (net < min_net) { 8 start = i + 1; 9 min_net = net; 10 } 11 } 12 return net >= 0 ? start : -1; 13 } 14 };
One final note, all of the above solutions are from this link in the LeetCode dicussion forum. Thank you for the nice sharer. Please refer to it for more details.