Minimum Number of Refueling Stops

Reach the target with the fewest refueling stops, deferring fuel choices via a max-heap of passed stations.

HARD
$8.99
heap
priority-queue
greedy
hanaokoro

By @hanaokoro

November 24, 2025

·

Updated May 18, 2026

404 views

2

4.5 (11)

I had this on a Doordash systems round and the interviewer's prompt was "do not commit to a station as you pass it". That hint is the entire algorithm: drive past every station you can reach, drop its fuel into a max-heap, and only when you actually run out of gas do you reach into the heap and grab the largest deposit you have seen. The catalog covers Greedy basics, but it skipped this defer-and-cash-in pattern.

Minimum Number of Refueling Stops

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.

Examples

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: Drive to position 10 with 0 gas. Refuel from 60 to bring the tank to 60. Drive to position 60 (50 miles, leaving 10 gas). Refuel from 40 to bring the tank to 50. Drive 40 more to the target.

Example 4:

  • Input: target = 100, startFuel = 50, stations = [[25, 25], [50, 25], [75, 25]]
  • Output: 2
  • Explanation: With one stop, max range is 50 + 25 = 75. With two stops we hit 75 + 25 = 100. Greedy buys the largest available fuel each time.

Constraints

  • 1 <= target <= 10^9.
  • 0 <= startFuel <= 10^9.
  • 0 <= stations.length <= 500.
  • 0 < stations[i][0] < stations[i + 1][0] < target (positions are strictly increasing and less than target).
  • 1 <= stations[i][1] <= 10^9.

Follow-up

Why is the greedy correct? Sketch: at any moment, all stations whose positions are within current reach are equivalent options. The minimum number of stops is achieved by always choosing the station with the LARGEST fuel remaining among reachable-but-not-yet-used stations.

Solution

Starter code, test cases, and solutions are locked.

Purchase this item to access the full workspace.

All Problems