Skip to main content

Minimum Number of Refueling Stops

LeetCode 902 | Difficulty: Hard​

Hard

Problem Description​

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] = [position~i~, fuel~i~] indicates that the i^th gas station is position~i~ miles east of the starting position and has fuel~i~ 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 <= 10^9`

- `0 <= stations.length <= 500`

- `1 <= position~i~ < position~i+1~ < target`

- `1 <= fuel~i~ < 10^9`

Topics: Array, Dynamic Programming, Greedy, Heap (Priority Queue)


Approach​

Dynamic Programming​

Break the problem into overlapping subproblems. Define a state (what information do you need?), a recurrence (how does state[i] depend on smaller states?), and a base case. Consider both top-down (memoization) and bottom-up (tabulation) approaches.

When to use

Optimal substructure + overlapping subproblems (counting ways, min/max cost, feasibility).

Greedy​

At each step, make the locally optimal choice. The challenge is proving the greedy choice leads to a global optimum. Look for: can I sort by some criterion? Does choosing the best option now ever hurt future choices?

When to use

Interval scheduling, activity selection, minimum coins (certain denominations), Huffman coding.


Solutions​

Solution 1: C# (Best: 128 ms)​

MetricValue
Runtime128 ms
Memory29.1 MB
Date2020-11-09
Solution
public class Solution {
public int MinRefuelStops(int target, int startFuel, int[][] stations) {
int m = stations.Length;
if(m==0) { return target<= startFuel ? 0 : -1 ;}
int n = stations[0].Length;
int[] dp = new int[m+1];
dp[0] = startFuel;

for (int i = 0; i < m; i++)
{
for (int t = i; t >= 0 ; --t)
{
if(dp[t] >= stations[i][0])
dp[t+1] = Math.Max(dp[t+1], dp[t]+stations[i][1]);
}
}

for (int t = 0; t <= m; t++)
{
if(dp[t] >= target) return t;
}
return -1;
}
}

Complexity Analysis​

ApproachTimeSpace
DP (2D)$O(n Γ— m)$$O(n Γ— m)$

Interview Tips​

Key Points
  • Break the problem into smaller subproblems. Communicate your approach before coding.
  • Define the DP state clearly. Ask: "What is the minimum information I need to make a decision at each step?"
  • Consider if you can reduce space by only keeping the last row/few values.