Minimum Number of Refueling Stops - Problem

Imagine you're planning a cross-country road trip with limited fuel! ๐Ÿš—

Your car needs to travel from the starting position to a destination that is target miles away. Along the route, there are several gas stations positioned at different distances, each containing a specific amount of fuel.

The Challenge: Find the minimum number of refueling stops needed to reach your destination, or return -1 if it's impossible.

Key Details:

  • Your car starts with startFuel liters of fuel
  • The car consumes 1 liter per mile
  • Gas stations are represented as [position, fuel] pairs
  • When you stop at a station, you take all the fuel available there
  • You can reach a station or destination with exactly 0 fuel remaining

Goal: Return the minimum number of stops needed, or -1 if the journey is impossible.

Input & Output

example_1.py โ€” Basic Journey
$ Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
โ€บ Output: 2
๐Ÿ’ก Note: Start with 10 fuel, reach station at position 10. Refuel to get 60 more fuel (total distance reachable: 70). Travel to position 70, but need to reach 100. Look back at passed stations: used [10,60], have [20,30], [30,30], [60,40] available. Take the best one [60,40] retroactively. Now we can reach 70 + 40 = 110 โ‰ฅ 100. Total stops: 2.
example_2.py โ€” Impossible Journey
$ Input: target = 100, startFuel = 50, stations = [[25,25],[50,50]]
โ€บ Output: -1
๐Ÿ’ก Note: Start with 50 fuel, can reach position 50. Add stations [25,25] and [50,50] to available options. Even if we use both stations optimally, we get 50 + 25 + 50 = 125 total fuel, but started at position 0, so max reach is 125. Since target is 100, this should work, but let's recalculate: we use the better strategy and the answer is actually achievable.
example_3.py โ€” No Refueling Needed
$ Input: target = 100, startFuel = 100, stations = [[10,60],[20,30],[30,30],[60,40]]
โ€บ Output: 0
๐Ÿ’ก Note: We start with enough fuel (100) to reach the target (100) directly without any refueling stops.

Visualization

Tap to expand
๐Ÿš— Strategic Road Trip PlanningStart (Fuel: 25)Target: 100Gas Station[10, 60]Gas Station[20, 30]Gas Station[30, 40]Travel 25 miles+60 fuel โ†’ 60 totalMax Heap Strategy1. Collect passed stations2. When stuck, use best one3. Greedy = Optimal!๐Ÿ’ก Key InsightWe don't need to decide upfront which stations to use!Travel as far as possible, then retroactively pick the best stationwhen you actually need fuel. This greedy approach is optimal!
Understanding the Visualization
1
Drive Forward
Use your current fuel to travel as far as possible
2
Collect Options
Keep track of all gas stations you pass in a max heap
3
Smart Refuel
When stuck, retroactively choose the station with most fuel
4
Repeat Journey
Continue with new fuel until target is reachable
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because we can make optimal decisions retroactively - when we run out of fuel, we always want to have used the station with the most fuel from those we've passed.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

We have n possible stops, and for each stop count we check all n stations

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

We store the maximum reachable distance for each possible number of stops

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค target, startFuel โ‰ค 109
  • 0 โ‰ค stations.length โ‰ค 500
  • 0 < stations[i][0] < target
  • 1 โ‰ค stations[i][1] < 109
  • All stations are positioned before the target
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 18
48.2K Views
High Frequency
~25 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