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
startFuelliters 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
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
โ Quadratic Growth
Space Complexity
O(n)
We store the maximum reachable distance for each possible number of stops
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code