Gas Station
Stations form a circle. Starting with an empty tank at i, collect gas[i] then spend cost[i] to reach the next station. Return the index from which one complete lap is possible, or -1.
Example 1
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]3Explanation
Starting at 3 accumulates enough surplus at 3 and 4 to cover deficits at 0,1,2.
Example 2
gas = [2,2]
cost = [3,3]-1Explanation
Total gas 4 is below total cost 6, so no start works.
Example 3
gas = [5]
cost = [4]0Explanation
Station 0 supplies 5 and costs 4, permitting the one-station lap.
Constraints
- 1 <= number of stations <= 100000
- gas and cost have equal lengths
- Values are nonnegative <= 10000
- If a solution exists its index is unique
Function contract
canCompleteCircuitArguments: json, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
[1,2,3,4,5][3,4,5,1,2]