Gas Station
There are n gas stations in a circle. gas[i] is the fuel available at station i and cost[i] is the fuel needed to travel from station i to station i+1 (wrapping around). Starting with an empty tank, return the index of the station from which you can complete the full circuit once in the clockwise direction, or -1 if impossible. If a solution exists it is guaranteed to be unique.
Open official problem prompt ↗Pick the one starting station (if any) from which a car can loop the whole circle without the fuel tank ever dropping below zero.
Like planning a road trip on a loop of gas stations: if you ever run dry between two stops, it is pointless to blame any station you already passed, so you simply declare the next station your new departure point and try again.
- Input
- gas = [1, 2, 3, 4, 5], cost = [3, 4, 5, 1, 2]
- Output
- 3
- Why
- Starting at station 3 the tank never goes negative and you return to station 3 with fuel to spare.
n == gas.length == cost.length1 <= n <= 10^50 <= gas[i], cost[i] <= 10^4If a solution exists, it is unique