Gas Station
Gas Station is LeetCode 134 (Medium). There are n gas stations on a circular route. Station i gives gas[i] fuel, and driving from station i to the next one costs cost[i]. Starting with an empty tank, return the index of the station from which you can drive all the way around once, clockwise, or -1 if no start works.
- If an answer exists, it is unique.
nis up to 10⁵, so trying every start (O(n²)) is too slow; the target is one pass.
- n == gas.length == cost.length
- 1 <= n <= 10⁵
- 0 <= gas[i], cost[i] <= 10⁴
- The input is generated such that the answer is unique.
Intuition
The gas station problem gets simple once you work with each station's gain, gas[i] - cost[i]. Two facts then give a one-pass greedy answer:
- Total: if all the gains add up to less than 0, the lap needs more fuel than exists, so no start works.
- Skip: if you start at
sand the tank first goes negative at stationi, no station fromstoican be the start either. You reached each of them with 0 or more fuel, so starting there instead can only arrive atiwith less.
So drive once along the array and restart just past every failure. If the total is not negative, the last restart is the answer.
A circular route with gains and costs, where you need a start whose running sum never drops below zero, is the gas station greedy: a failed run rules out every start inside it. Kadane's algorithm for Maximum Subarray uses the same move of dropping a negative prefix and starting fresh.
Approach
Before reading on: with gas = [1,2,3,4,5] and cost = [3,4,5,1,2], write the gain at each station. If a start fails at station 2, could any station before 2 have been the answer?
Keep two sums
total adds every gain and is never reset; it only decides whether any answer exists. tank is the fuel since the current candidate start, and resets when that candidate fails. Keep them apart: they answer different questions.
Add each station's gain
For each station, compute gain = gas[i] - cost[i] and add it to both sums. While tank stays 0 or more, the car from start can reach the next station, so the candidate survives. A tank of exactly 0 is fine: the car arrives empty.
Restart after a failure
If tank < 0, the car from start cannot get past station i. Every station between start and i would arrive with even less fuel, so skip all of them at once: set start = i + 1 and tank = 0.
Return the candidate
After one pass, if total < 0, return -1. Otherwise return start. The car from start reached the end of the array, and because the total is not negative, the fuel it carries covers the stations before start on the way back round.
Gas Station solution in Python | C++ | Java
tank follows the car from the current candidate start; total adds every gain and is never reset.total keeps counting.total keeps counting.total keeps counting.tank follows the car from the current candidate start; total adds every gain and is never reset.total keeps counting.total keeps counting.tank follows the car from the current candidate start; total adds every gain and is never reset.total keeps counting.total keeps counting.Common pitfalls
Returning start without checking the total
return start
return start if total >= 0 else -1
For gas = [2, 3, 4], cost = [3, 4, 3] the scan ends with start = 2, but the gains add up to −1, so no start can finish the lap and the answer is −1.
Restarting at the failing station
start = i
start = i + 1
The tank turns negative at station i only if station i's own gain is negative, so starting there fails straight away. The next real candidate is the station after it.
Moving start but keeping the old tank
if tank < 0:
start = i + 1if tank < 0:
start = i + 1
tank = 0The negative tank belongs to the old candidate. Carrying it over makes the new start look worse than it is, so the scan can skip past the real answer.
Edge cases
gas = [1,2,3,4,5], cost = [3,4,5,1,2]The car finishes the lap with an empty tank, which still counts. Test total >= 0, not total > 0; the answer here is 3.
Complexity
Gas Station FAQ
Can you solve the gas station LeetCode problem by trying every start?
Yes: from each station, simulate the lap and stop when the tank goes negative. That is O(n²), up to 10¹⁰ steps for 10⁵ stations, which times out. The greedy pass skips every failed start at once and runs in O(n).