aboutsummaryrefslogtreecommitdiff
path: root/min_num_refueling_stops.cpp
blob: 34d929b66b74df17a4f25dbc17acf094aca2d1c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "leetcode.h"

class Solution {
public:
  int minRefuelStops(int target, int startFuel, vector<vector<int>> &stations) {
    int n = stations.size(), ans = 0;
    priority_queue<int> pq;
    for (int i = 0; i <= n; i++) {
      int dist = i == n ? target : stations[i][0];
      while (startFuel < dist) {
        if (!pq.size())
          return -1;
        startFuel += pq.top();
        ans++;
        pq.pop();
      }
      if (i < n)
        pq.push(stations[i][1]);
    }
    return ans;
  }
};