Gas Station - Problem

Imagine you're on a road trip around a circular highway with n gas stations positioned along the route. Each gas station i has a specific amount of fuel available: gas[i] gallons.

Your car has an unlimited fuel tank but starts completely empty. The catch? It costs cost[i] gallons of fuel to drive from station i to the next station (i+1). Since the route is circular, after the last station, you return to the first one.

Your mission: Find a starting gas station where you can complete the entire circular journey exactly once, or determine if it's impossible.

Input: Two arrays gas[] and cost[] of length n
Output: The index of the starting gas station, or -1 if no solution exists
Guarantee: If a solution exists, it's unique!

Input & Output

example_1.py โ€” Standard Case
$ Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
โ€บ Output: 3
๐Ÿ’ก Note: Starting at station 3: fuel=4, can reach station 4 (cost=1). At station 4: fuel=3+5=8, can reach station 0 (cost=2). At station 0: fuel=6+1=7, can reach station 1 (cost=3). Continue this way to complete the circuit.
example_2.py โ€” Impossible Case
$ Input: gas = [2,3,4], cost = [3,4,3]
โ€บ Output: -1
๐Ÿ’ก Note: Total gas available is 2+3+4=9, but total cost needed is 3+4+3=10. Since total gas < total cost, it's impossible to complete the circuit from any starting point.
example_3.py โ€” Edge Case Single Station
$ Input: gas = [5], cost = [4]
โ€บ Output: 0
๐Ÿ’ก Note: With only one station, we start there, gain 5 fuel, and need 4 to return to the same station. Since 5 โ‰ฅ 4, we can complete the circuit starting from station 0.

Constraints

  • gas.length == n
  • cost.length == n
  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค gas[i], cost[i] โ‰ค 104
  • The solution is guaranteed to be unique if it exists

Visualization

Tap to expand
Circular Gas Station HighwayS01/3Net: -2S12/4Net: -2S23/5Net: -2S34/1Net: +3S45/2Net: +3S5Back to S0โŒ Deficit stationsโœ“ Surplus stations๐ŸŽฏ Optimal path from S3Key Insight:Start after the last deficit point!
Understanding the Visualization
1
Check Total Feasibility
First, verify if the total fuel available across all stations is enough to cover the total cost of the entire journey
2
Track Fuel Balance
Start from station 0 and keep track of your fuel balance as you visit each station
3
Reset on Fuel Shortage
Whenever you run out of fuel, you know that station (and all previous ones) cannot be the starting point
4
Find Optimal Start
The first station after your last fuel shortage becomes your candidate starting point
5
Guarantee Solution
If total fuel โ‰ฅ total cost, this candidate is guaranteed to work due to the circular nature
Key Takeaway
๐ŸŽฏ Key Insight: The optimal starting point is always right after the last station where we run out of fuel, because that's where the fuel surplus begins!
Asked in
Amazon 45 Google 38 Meta 29 Microsoft 22
48.2K Views
High Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen