Skip to problem
←

Minimum Number of Refueling Stops

Hard 0 attempts

A car begins a trip with startFuel units of fuel in its tank and must travel a distance of target. Along the route sit several gas stations, each given as a [position, fuel] pair describing its location and how much fuel it adds to the tank. One unit of fuel covers one unit of distance. Work out the fewest refueling stops needed to reach (or pass) target, or return -1 if the car can't make it.

The tank has unlimited capacity. At a station you may take all its fuel once or skip it. Reaching a station or the destination with exactly zero fuel is allowed. You may only take fuel from stations you can reach; passing a station without refueling does not count as a stop.

In Example 1, take 60 units at position 10 and 40 units at position 60. These two stops provide enough fuel to reach the target.

Function contract

Implement minRefuelStops(target, startFuel, stations). The car starts at position 0; stations contains [position, availableFuel] pairs in route order.

Output: Return the minimum number of stations where fuel must be taken, or -1 if the target is unreachable. Return 0 if starting fuel is sufficient.

Example 1

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

Example 2

Input
target = 1
startFuel = 1
stations = []
Output
0

Example 3

Input
target = 100
startFuel = 1
stations = [[10,100]]
Output
-1
Constraints
  • 1 <= target <= 10^9
  • 0 <= startFuel <= 10^9
  • 0 <= stations.length <= 500
  • Stations have strictly increasing positions with 1 <= position < target and 1 <= fuel <= 10^9
Topics

Greedy with Max-Heap

Treat fuel as the farthest distance reachable from the origin using the fuel selected so far, not the fuel currently left in a moving car. Add every station within that reach to a max-heap. If the target is still beyond reach, take the largest available fuel amount and count one stop. If none is available, the trip is impossible.

This retrospectively chooses which reachable station to stop at; it does not require driving backward. Among candidates already reachable, the largest fuel amount extends reach the most for one extra stop, so choosing it cannot require more stops than choosing a smaller amount. Each station enters and leaves the heap at most once.

javascript
function minRefuelStops(target, startFuel, stations) {

  const heap = [];
  function push(value) {
    heap.push(value); let i = heap.length - 1;
    while (i > 0) {
      const p = Math.floor((i-1)/2); if (heap[p] >= heap[i]) break;
      [heap[p],heap[i]] = [heap[i],heap[p]]; i = p;
    }
  }
  function pop() {
    const max = heap[0], last = heap.pop();
    if (heap.length) {
      heap[0] = last; let i = 0;
      while (2*i+1 < heap.length) {
        let child = 2*i+1;
        if (child+1 < heap.length && heap[child+1] > heap[child]) child++;
        if (heap[i] >= heap[child]) break;
        [heap[i],heap[child]] = [heap[child],heap[i]]; i = child;
      }
    }
    return max;
  }
  let fuel = startFuel, stops = 0, i = 0;
  while (fuel < target) {
    while (i < stations.length && stations[i][0] <= fuel) push(stations[i++][1]);
    if (!heap.length) return -1;
    fuel += pop(); stops++;
  }
  return stops;
}

Time: O(n log n) | Space: O(n)

Loading discussion…

Saved in this browser only. Private to you.

JavaScript