Skip to content

Minimum Number of Refueling Stops

HardHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.

A car drives east toward a destination target miles away. It starts with start_fuel (startFuel in TypeScript) liters, burns one liter per mile, and has an unlimited tank. Along the way are gas stations stations[i] = [position_i, fuel_i]: stopping there adds fuel_i liters.

Return the minimum number of refueling stops needed to reach the target, or -1 if it cannot be reached. Arriving somewhere with exactly 0 fuel left still counts as arriving.

target = 100, start_fuel = 10, stations = [[10, 60], [20, 30], [30, 30], [60, 40]]  →  2
target = 100, start_fuel = 1,  stations = [[10, 100]]                               →  -1
target = 1,   start_fuel = 1,  stations = []                                        →  0

In the first example, stop at mile 10 (60 liters, reach mile 70), then at mile 60 (40 liters, reach mile 110).

Constraints

  • 1 <= target, start_fuel <= 10^9
  • 0 <= stations.length <= 500
  • Station positions strictly increase and are all below target.
  • 1 <= fuel_i <= 10^9