Likou Brushing Notes: 134. Gas Station

Subject : 134. Gas Station
There are N gas stations on a ring road, and the i-th gas station has gasoline gas[i] liters.

You have a car with an unlimited fuel tank. Driving from the i-th gas station to the i+1-th gas station requires cost[i] liters of gasoline. You start from one of the gas stations and the gas tank is empty at the beginning.

If you can go around the loop, return the number of the gas station when you started, otherwise return -1.


If the question has a solution, that answer is the only answer.
The input arrays are all non-empty arrays and have the same length.
The elements in the input array are all non-negative numbers.
Example 1:

gas = [1,2,3,4,5]
cost = [3,4,5,1,2]

Output: 3

Starting from No. 3 gas station (index is 3), you can get 4 liters of gasoline. At this time, the fuel tank has = 0 + 4 = 4 liters of petrol going
to the No. 4 gas station. At this time, there are 4-1 + 5 = 8 liters of petrol going
to the No. 0 gas station. At this time, the fuel tank has 8-2 + 1 = 7 Liter of petrol
to the No. 1 gas station. At this time, the fuel tank has 7-3 + 2 = 6 liters of petrol
to the No. 2 gas station. At this time, the fuel tank has 6-4 + 3 = 5 liters of petrol
to the No. 3 gas station. Need to consume 5 liters of gasoline, just enough for you to return to the No. 3 gas station.
Therefore, 3 can be the starting index.


  • Main idea: Greedy
  • At the beginning, the best standard I was looking for was the largest difference between gas-cost as the standard, and then started with this to verify whether it could make a round, and use (reserved oil) as the condition for jumping out of the loop. The two test cases given by the question have passed, but I still know that it is still wrong. If you are the one with the largest difference, but the next one is the one with the largest difference and contrast, the words will jump out directly. Lead to a landed error

After watching the analysis given by Likou official website , I feel that I have learned a lot. The following is a self-summary

  • First of all, there is one thing to understand. If total_gas-total_cost <0, it is impossible to complete a turn.
  • Traverse the array once, starting from 0, and the initial starting point start = 0
  • Calculate total_tank, which is the difference, and finally used to judge
  • And calculate currt_bank, and judge whether currt_bank is less than 0. If yes, it means that the previous start cannot be used as the starting point, and the starting point needs to be changed. (It is impossible to loop the front, because from start to i , currt_bank is greater than or equal to 0, When it comes to i , it does not match), so we should use i as a new starting point. , And re-zero currt_bank, and assign start.
  • Finally, it is judged whether total_bank is greater than or equal to zero. If it is true, it returns start, otherwise it returns -1.

Source code

class Solution {
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int n = gas.size();

        int total_gas = 0;
        int currt_gas = 0;
        int start = 0;
        for(int i = 0; i < n; ++i){
            total_gas += gas[i] - cost[i];
            currt_gas += gas[i] - cost[i];
            if(currt_gas < 0){
                start = i + 1;
                currt_gas = 0;
        return total_gas >= 0 ? start: -1;

Guess you like