# LeetCode – Gas Station (Java)

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.

Analysis

To solve this problem, we need to understand and use the following 2 facts:
1) if the sum of gas >= the sum of cost, then the circle can be completed.
2) if A can not reach C in a the sequence of A–>B–>C, then B can not make it either.

Proof of fact 2:

```If gas[A] < cost[A], then A can not even reach B.
So to reach C from A, gas[A] must >= cost[A].
Given that A can not reach C, we have gas[A] + gas[B] < cost[A] + cost[B],
and gas[A] >= cost[A],
Therefore, gas[B] < cost[B], i.e., B can not reach C.
```

In the following solution, sumRemaining tracks the sum of remaining to the current index. If sumRemaining < 0, then every index between old start and current index is bad, and we need to update start to be the current index. You can use the following example to visualize the solution.

Java Solution

```public int canCompleteCircuit(int[] gas, int[] cost) { int sumRemaining = 0; // track current remaining int total = 0; // track total remaining int start = 0;   for (int i = 0; i < gas.length; i++) { int remaining = gas[i] - cost[i];   //if sum remaining of (i-1) >= 0, continue if (sumRemaining >= 0) { sumRemaining += remaining; //otherwise, reset start index to be current } else { sumRemaining = remaining; start = i; } total += remaining; }   if (total >= 0){ return start; }else{ return -1; } }```

### 9 thoughts on “LeetCode – Gas Station (Java)”

1. } else {
sumRemaining = remaining;
start = i;
}
——-

something wrong /w code.

why we do even consider station w/ neg gas-cost?

obv solution starts w/ positive station (gas minus cost)

so keep adding stations, if sumremaining is neg, skip all neg stations and start over.

2. West, why do you think the algorithm in the question doesn’t work?

3. the solution is correct and elegeant

4. Your solution doesn’t solve the problem. You are not given a starting station, you are asked to find a starting station such that its index is minimal and you can travel around the circle once, e.g. https://www.interviewbit.com/problems/gas-station/

5. ``` public static int canCompleteCircuit(int[] gas, int[] cost,int start) { int i = start; int fuel = 0; do{ if(start==0 && i==gas.length){i=0;break;} if(i==gas.length)i=0; fuel += gas[i]-cost[i]; if(fuel<0)return -1; ++i; }while (i!=start); return i; } ```

6. algorithm do not work on
gas 0,4,3 and cost 2,4,1
obviously that if we start at station 0 with no fuel we cant race circle
but when we start at station #1 with 4 fuel we can race

7. Hi your solution does not work for these inputs:

int [] gas= {1,2,3,4,5};
int [] cost= {1,3,3,4,5};

8. Check this simpler solution.

public static int findStart(int[] gas, int[] cost) {

if (gas.length != cost.length) {

return -1;

}

int remaining = 0, start = 0;

for (int i = 0; i < gas.length; i++) {

remaining += gas[i] – cost[i];

if (remaining < 0) {

start = i + 1;

remaining = 0;

}

}

return start;

}