medium
Gas Station
Along a circular route there are n stations; station i provides gas[i] fuel and it costs cost[i] fuel to drive to the next station. Starting with an empty tank, return the index of the station from which you can complete a full loop, or -1 if impossible. The answer is unique when it exists.
Constraints
- 1 ≤ n ≤ 10^5
- 0 ≤ gas[i], cost[i] ≤ 10^4
Examples
in: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
out: 3
Code it yourself
Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.