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. Need 50 more fuel to reach target 100. Can use station [25,25] retroactively - with 25 additional fuel, can reach 50+25=75. Can also use station [50,50] retroactively - with 50 additional fuel, can reach 50+50=100. Choose [50,50] for 1 total stop.
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.

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

Visualization

Tap to expand
Minimum Number of Refueling Stops INPUT Start Target 10 20 30 60 target = 100 startFuel = 10 Stations Array: [10, 60] pos:10, fuel:60 [20, 30] pos:20, fuel:30 [30, 30] pos:30, fuel:30 [60, 40] pos:60, fuel:40 = Gas Station = Car ALGORITHM STEPS 1 Initialize fuel=10, stops=0, maxHeap=[] 2 Travel to station 10 fuel: 10-10=0, add 60 to heap 3 Refuel from heap (60) fuel=60, stops=1 4 Continue to stations Pass 20,30,60 - add to heap Max Heap State: 40 30 30 5 Refuel at pos 60 Take 40 from heap, stops=2 fuel=40, reach target 100 FINAL RESULT STOP STOP TARGET OUTPUT 2 Minimum stops needed Journey Breakdown: 1. Start with 10 fuel 2. Stop at pos 10 (get 60) 3. Pass stations 20, 30 4. Stop at pos 60 (get 40) 5. Reach target 100 - OK Key Insight: Use a Max Heap to store fuel from passed stations. When you run out of fuel, greedily take the maximum fuel available from the heap. This simulates "going back" to the best station. Time Complexity: O(n log n) | Space Complexity: O(n) where n = number of stations TutorialsPoint - Minimum Number of Refueling Stops | Greedy with Max Heap (Optimal)
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