Gas Station
There are n gas stations in a circle. You have gas[i] fuel at station i and it costs cost[i] fuel to travel to the next station.
Return the starting station index if you can travel around the circuit once clockwise, or -1 if impossible.
If a solution exists, it is guaranteed to be unique.
Example
1,2,3,4,5 3,4,5,1,2
3
gasis the fuel collected at each station;costis the fuel burned driving from station to station (station 4 wraps back to 0). The tank starts empty.- Net change per stop, : . A start works only if the tank never drops below during the lap.
- Stations 0, 1 and 2 fail at once: station 0 gives unit but the leg needs .
- From station 3 the tank after each leg is , , , , . Never negative, so the answer is
3. - Station 4 fails later: , , then between stations 1 and 2.
- Notice total gas equals total cost , so the successful lap ends with an exactly empty tank.
Constraints
- 1 <= n <= 10^5
- 0 <= gas[i], cost[i] <= 10^4