Skip to content
Decoder

Minimum Number of Refueling Stops

Hard

Problem

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.

Example 1

Input
target = 100, start_fuel = 10, stations = [[10, 60], [20, 30], [30, 30], [60, 40]]
Output
2

Example 2

Input
target = 100, start_fuel = 1, stations = [[10, 100]]
Output
-1

Example 3

Input
target = 1, start_fuel = 1, stations = []
Output
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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.