Gas Station
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.
Note:
The solution is guaranteed to be unique.
Explanation:
- If the sum of gas is smaller than the sum of cost, then there is no solution. Otherwise, there must be a solution
- The question guarantees that the solution is unique
- The tank should never be negative
- If a car from A cannot reach C and C is the first station that A cannot reach, then any station between A and C cannot reach C either. Thus, the car can start from the next station after C.
- See here for proof
1 | public class Solution { |