A car travels from a starting position to a destination which is target miles east of the starting position.
There are gas stations along the way. The gas stations are represented as an array stations where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east of the starting position and has fueli liters of gas.
The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.
Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1.
Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have arrived.
Example 1:
Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling.
Example 2:
Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can not reach the target (or even the first gas station).
Example 3:
Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.
Constraints:
1 <= target, startFuel <= 1090 <= stations.length <= 5001 <= positioni < positioni+1 < target1 <= fueli < 109The Minimum Number of Refueling Stops problem asks you to determine the least number of stops required to reach a target distance with limited starting fuel and several fuel stations along the way. A key observation is that you should always refuel from the stations that give the largest fuel benefit when needed.
A common strategy uses a greedy approach with a max heap. As you travel toward the target, add reachable stations' fuel amounts into a max-heap. Whenever your current fuel is insufficient to move forward, refuel from the station with the largest fuel amount stored in the heap. This ensures the minimal number of stops while maximizing distance coverage.
Another perspective uses dynamic programming, where dp[i] represents the farthest distance reachable using exactly i refueling stops. For each station, update the DP states in reverse order to maintain correctness.
The greedy heap approach is typically preferred in interviews due to its efficiency and clean logic.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Greedy with Max Heap | O(n log n) | O(n) |
| Dynamic Programming | O(n^2) | O(n) |
NeetCode
This approach leverages a greedy strategy with a max heap to ensure that whenever we're out of fuel, we choose to refuel from the station that offers the most fuel, maximizing our reach.
We traverse through the list of stations, when we reach a station, we check if we need to refuel (i.e., if our current fuel can't get us to the next station). If so, we refuel from the stations we've passed (choosing the one with the most fuel available). We keep track of the number of stops and return that as our result once we reach the target, or return -1 if it's not possible to reach the target.
Time Complexity: O(n log n), as each insertion into the heap takes O(log n), and we perform this operation n times.
Space Complexity: O(n), storing the maximum fuel to be reused in a max-heap equivalent array.
1#include <stdio.h>
2#include <stdlib.h>
3
4int minRefuelStops(int target, int startFuel, int*
This C solution uses an array to mimic a max-heap, replacing the max-heap functionality with a manually sorted insertion. Fuel from each station is added to the heap whenever it is reachable. We keep refueling from the station with maximum fuel until we reach the target or run out of options.
In this approach, we use dynamic programming to track the maximum distance reached with a given number of refuel stops. By simulating the range expansions step-by-step at each station, we keep updating our maximum reach for each refuel stop count.
This works by creating an array dp where dp[i] stands for the farthest distance we can reach with i stops. Initially, dp[0] is initialized with startFuel, and the rest are kept at zero. As we iterate through the stations, we update this dp array backwards so as not to overwrite data prematurely. For each station, we update the dp array to reflect the best possible distance for each number of stops, which includes potentially refueling at that station.
Time Complexity: O(n^2), as each station iterates over previous positions in potentially worst-case scenarios.
Space Complexity: O(n), where n is the station count and the dp array stores potential distances.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, this problem is considered a classic greedy and heap-based interview question. Variations of it appear in interviews at companies like Google, Amazon, and Meta to test optimization strategies and priority queue usage.
A max heap or priority queue is the most effective data structure for this problem. It allows quick access to the station with the highest fuel amount among all reachable stations. This makes greedy refueling decisions efficient.
The optimal approach typically uses a greedy strategy combined with a max heap (priority queue). As you pass stations, you store their fuel amounts and refuel only when necessary from the station with the maximum fuel. This ensures the minimum number of stops while maintaining efficient performance.
Yes, a dynamic programming approach can track the maximum distance reachable with a certain number of stops. While correct, it has higher time complexity compared to the greedy heap approach, so it is usually discussed as an alternative solution.
1class Solution {
2 public int minRefuelStops(int target, int startFuel, int[][] stations) {
3 long[] dp = new long[stations.length + 1];
4 dp[0] = startFuel;
5
6 for (int i = 0; i < stations.length; ++i) {
7 for (int t = i; t >= 0; --t) {
8 if (dp[t] >= stations[i][0])
9 dp[t + 1] = Math.max(dp[t + 1], dp[t] + stations[i][1]);
10 }
11 }
12
13 for (int i = 0; i <= stations.length; ++i) {
14 if (dp[i] >= target) return i;
15 }
16 return -1;
17 }
18}
19Using Java, the 1D dp array structure dynamically calculates furthest drives with a variety of fuel stops, evaluated at each station. The preservation of station-wise maximum distances gives insights into the fewest needed stops, if achievable, to reach desired targets.