Greedy Starting Point for a Circular Fuel Route
Greedy & IntervalsAuto-graded · 20 test casesLast updated
Tests whether a candidate can apply a greedy one-pass argument to find the unique valid departure station in a circular gas-station problem.
Problem
You are planning a road trip along a circular route with n stops numbered 0 through n - 1. At each stop i you can pick up gas[i] litres of fuel. Driving from stop i to the next stop (i + 1) % n burns exactly cost[i] litres.
Your tank starts empty when you depart. You may choose any stop as your departure point. Find the index of the only stop from which you can complete the full loop without your fuel tank ever going negative. If no such stop exists, return -1.
You are guaranteed that if a solution exists, it is unique.
Rules
- You travel clockwise only: from stop
iyou always proceed to stop(i + 1) % n. - Before leaving a stop you fill up with that stop's fuel, then spend fuel driving to the next stop.
- Your tank may reach exactly
0— that is still valid. - The answer is unique when it exists.
Constraints
1 <= n <= 10^50 <= gas[i], cost[i] <= 10^4gasandcosthave the same lengthn.
Examples
Example 1
Input: find_departure_stop([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])
Output: 3
Example 2
Input: find_departure_stop([2, 3, 4], [3, 4, 3])
Output: -1
Example 3
Input: find_departure_stop([5], [5])
Output: 0
Hints
Hint 1
Compute the net gain or loss at each stop. If the total across all stops is negative, no starting point can work — return -1 immediately.
Hint 2
Simulate a journey from a candidate start, keeping a running tank balance. If the balance ever goes negative, can any stop between your candidate and the current stop be a better starting point? Think about why not.
Hint 3
Once you understand why a negative balance forces you to abandon every stop up to and including the current one, the rest of the algorithm writes itself in a single loop with O(1) extra space.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core observations
Define balance[i] = gas[i] - cost[i] — the net fuel change at stop i.
Two facts together give an O(n) solution.
Fact 1 — feasibility check.
If sum(balance) is negative, the route consumes more fuel than it provides in total. No starting point can fix that, so return -1 immediately.
Fact 2 — greedy elimination.
Simulate a journey from a candidate start, accumulating a running tank. The moment the running tank drops below zero at stop i, every stop from the current candidate up to and including i is disqualified:
- If starting from the original candidate failed at
i, starting from any later stopjin that range means skipping a prefix that was non-negative (the tank only went negative ati, not earlier). Skipping a non-negative prefix cannot raise the tank on arrival ati, sojalso fails at or beforei.
So we can safely jump the candidate forward to i + 1 and reset the running tank to zero, without missing any valid answer.
Why the surviving candidate is correct
After the loop, if total >= 0, exactly one candidate remains. The stops before it each triggered a reset, meaning their segments had negative prefix sums — none of them can be the answer. The final candidate's segment (from start to n - 1) accumulated a non-negative running total. Because total >= 0, that surplus is large enough to cover the wrap-around portion (stops 0 through start - 1), so the candidate successfully completes the full loop.
Edge cases
- n = 1: returns
0whengas[0] >= cost[0], otherwise-1— handled naturally. - All zeros:
total = 0 >= 0, returns0. - Exact balance: a running tank that touches zero but never goes negative is still valid.
Complexity
- Time: O(n) — one pass through both arrays.
- Space: O(1) — three scalar variables.
Reference solution
from typing import List
def find_departure_stop(gas: List[int], cost: List[int]) -> int:
total = 0
current = 0
start = 0
for i, (fuel, expense) in enumerate(zip(gas, cost)):
balance = fuel - expense
total += balance
current += balance
if current < 0:
start = i + 1
current = 0
return start if total >= 0 else -1