dsa · medium
Gas Station
There are n stations in a circle. gas[i] is the fuel at station i, cost[i] is the fuel to go from i to i+1 (and from n-1 back to 0).
Arguments
gas— fuel available at each stationcost— fuel to drive from this station to the next (circular)
Start with an empty tank. Return the starting station index from which you can travel the full circle once, or -1 if impossible. If an answer exists it is unique.
Example
gas = [1,2,3,4,5], cost = [3,4,5,1,2] → 3.
gas = [2,3,4], cost = [3,4,3] → -1.
Constraints
n == gas.length == cost.length 1 <= n <= 10^5 0 <= gas[i], cost[i] <= 10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,2,3,4,5] [3,4,5,1,2] Expected: 3
Example 2
Input: [2,3,4] [3,4,3] Expected: -1