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
20
class Solution {
 public:
  int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
    // dp[i] := farthest position we can reach w/ 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])  // with j - 1 refuels, we can reach 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] := farthest position we can reach w/ 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]) // with j - 1 refuels, we can reach 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
class Solution:
  def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
    # dp[i] := 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]:  # with j - 1 refuels, we can reach stations[i][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's index
    int curr = startFuel;
    priority_queue<int> maxHeap;

    while (curr < target) {
      // add all reachable stops to maxHeap
      while (i < stations.size() && curr >= stations[i][0])
        maxHeap.push(stations[i++][1]);
      if (maxHeap.empty())  // we can't refuel
        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's index
    int curr = startFuel;
    Queue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

    while (curr < target) {
      // add all reachable stops to maxHeap
      while (i < stations.length && curr >= stations[i][0])
        maxHeap.offer(stations[i++][1]);
      if (maxHeap.isEmpty()) // we can't refuel
        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
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 reachable stops to maxHeap
      while i < len(stations) and stations[i][0] <= curr:
        heapq.heappush(maxHeap, -stations[i][1])
        i += 1
      if not maxHeap:  # we can't refuel
        return -1
      curr -= heapq.heappop(maxHeap)  # pop out the largest gas
      ans += 1  # then refuel once

    return ans
Back to top