Skip to content

871. Minimum Number of Refueling Stops 👍

Approach 1: DP

  • Time: $O(n^2)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
    // dp[i] := the farthest position we can reach with i refuels
    vector<long> dp(stations.size() + 1);
    dp[0] = startFuel;

    for (int i = 0; i < stations.size(); ++i)
      for (int j = i + 1; j > 0; --j)
        if (dp[j - 1] >= stations[i][0])
          dp[j] = max(dp[j], dp[j - 1] + stations[i][1]);

    for (int i = 0; i < dp.size(); ++i)
      if (dp[i] >= target)
        return i;

    return -1;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
  public int minRefuelStops(int target, int startFuel, int[][] stations) {
    // dp[i] := the farthest position we can reach with i refuels
    long dp[] = new long[stations.length + 1];
    dp[0] = startFuel;

    for (int i = 0; i < stations.length; ++i)
      for (int j = i + 1; j > 0; --j)
        if (dp[j - 1] >= stations[i][0])
          dp[j] = Math.max(dp[j], dp[j - 1] + stations[i][1]);

    for (int i = 0; i < dp.length; ++i)
      if (dp[i] >= target)
        return i;

    return -1;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
  def minRefuelStops(
      self,
      target: int,
      startFuel: int,
      stations: list[list[int]],
  ) -> int:
    # dp[i] := the farthest position we can reach w / i refuels
    dp = [startFuel] + [0] * len(stations)

    for i, station in enumerate(stations):
      for j in range(i + 1, 0, -1):
        if dp[j - 1] >= station[0]:
          dp[j] = max(dp[j], dp[j - 1] + station[1])

    for i, d in enumerate(dp):
      if d >= target:
        return i

    return -1

Approach 2: Heap

  • Time: $O(n\log n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
 public:
  int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
    int ans = 0;
    int i = 0;  // stations' index
    int curr = startFuel;
    priority_queue<int> maxHeap;

    while (curr < target) {
      // Add all the reachable stops to maxHeap
      while (i < stations.size() && curr >= stations[i][0])
        maxHeap.push(stations[i++][1]);
      if (maxHeap.empty())  // Can't be refueled.
        return -1;
      curr += maxHeap.top(), maxHeap.pop();  // Pop out the largest gas.
      ++ans;                                 // Then, refuel once.
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
  public int minRefuelStops(int target, int startFuel, int[][] stations) {
    int ans = 0;
    int i = 0; // stations' index
    int curr = startFuel;
    Queue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

    while (curr < target) {
      // Add all the reachable stops to maxHeap
      while (i < stations.length && curr >= stations[i][0])
        maxHeap.offer(stations[i++][1]);
      if (maxHeap.isEmpty()) // Can't be refueled.
        return -1;
      curr += maxHeap.poll(); // Pop out the largest gas.
      ++ans;                  // Then, refuel once.
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
  def minRefuelStops(
      self,
      target: int,
      startFuel: int,
      stations: list[list[int]],
  ) -> int:
    ans = 0
    i = 0  # station's index
    curr = startFuel
    maxHeap = []

    while curr < target:
      # Add all the reachable stops to maxHeap
      while i < len(stations) and stations[i][0] <= curr:
        heapq.heappush(maxHeap, -stations[i][1])
        i += 1
      if not maxHeap:  # Can't be refueled.
        return -1
      curr -= heapq.heappop(maxHeap)  # Pop out the largest gas.
      ans += 1  # Then, refuel once.

    return ans