Gas Station
Master this topic with zero to advance depth.
Gas Station
There are n gas stations along a circular route, where the amount of gas at the station is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the station to its next station.
Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
Circular Logic
If you start at index , you must be able to visit .
Examples
Start at station 3 (index 3). Fill 4 units of gas. Your tank = 4. Cost to next = 1. Remaining = 3. Final tank check covers the loop.
Level I: Brute Force (Simulation)
Intuition
Try starting from every single gas station and simulate the journey. If you can complete the circle without the tank becoming negative, that's your starting station.
Visual Representation
Stations: [S0, S1, S2]
Try S0: S0 -> S1 -> S2 -> S0 (Check tank >= 0 at each step)
Try S1: S1 -> S2 -> S0 -> S1
Try S2: S2 -> S0 -> S1 -> S2Thought Process
- Iterate
startfrom to . - For each
start, initializetank = 0. - Iterate
ifrom to to visit all stations using(start + i) % n. - If
tank < 0at any point, stop and try nextstart.
Pattern: Circular Simulation
Detailed Dry Run
gas=[1,2], cost=[2,1] S0: tank=1-2=-1. FAIL. S1: tank=2-1=1. Next: tank=1+(1-2)=0. SUCCESS!
Level II: Greedy with Total Sum Check
Intuition
Before attempting a simulation, check if a solution even exists. If , it is mathematically impossible to complete the circuit regardless of where you start.
Thought Process
- Calculate
totalGasandtotalCost. - If
totalGas < totalCost, return -1. - Otherwise, use the greedy property: if you fail to reach station starting from , any station between and will also fail to reach .
Pattern: Pruning / Feasibility Check
Detailed Dry Run
gas=[1,2,3], cost=[2,2,2]. Total gas=6, cost=6. Possible. Start S0: tank=1-2=-1. FAIL. Reset start to S1. Start S1: tank=2-2=0. OK. Start S2: tank=0+(3-2)=1. OK. Return S1.
Level III: Optimal Greedy (One-Pass)
Intuition
We can combine the total sum check into the main loop. Track totalDiff as we go. If totalDiff is negative at the end, return -1. Otherwise, the last valid start candidate is guaranteed to work.
Visual Representation
Indices: [ 0, 1, 2, 3, 4]
Diffs: [-2, -2, -2, 3, 3]
Loop:
i=0: tank=-2. Fail. start=1, tank=0.
i=1: tank=-2. Fail. start=2, tank=0.
i=2: tank=-2. Fail. start=3, tank=0.
i=3: tank=3. OK.
i=4: tank=6. OK.
TotalDiff = 0 >= 0. Result: start index 3.Thought Process
- Use
totalTankto keep running sum of all(gas[i] - cost[i]). - Use
currentTankto track gas from currentstartcandidate. - If
currentTankdrops below 0, the currentstart(and all stations between it andi) is invalid. ResetcurrentTankand setstart = i + 1. - Final answer:
totalTank >= 0 ? start : -1.
Pattern: Candidate Elimination
Detailed Dry Run
gas=[1,2,3,4,5], cost=[3,4,5,1,2]
| i | diff | curTank | totalTank | start |
|---|---|---|---|---|
| 0 | -2 | -2 (Reset) | -2 | 1 |
| 1 | -2 | -2 (Reset) | -4 | 2 |
| 2 | -2 | -2 (Reset) | -6 | 3 |
| 3 | 3 | 3 | -3 | 3 |
| 4 | 3 | 6 | 0 | 3 |
| totalTank=0. Return 3. |
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.