871. Minimum Number of Refueling Stops
Implementation
```cpp
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector>& stations) {
int n = stations.size();
priority_queue pq;
int prev = 0;
int tank = startFuel;
int count = 0;
for (int i = 0; i < n; i++) {
int position = stations[i][0];
int fuel = stations[i][1];
tank -= (position - prev);
while (!pq.empty() && tank < 0) {
tank += pq.top();
pq.pop();
count ++;
}
if (tank < 0) {
return -1;
}
pq.push(fuel);
prev = position;
}
tank -= (target - prev);
while (!pq.empty() && tank < 0) {
tank += pq.top();
pq.pop();
count ++;
}
if (tank < 0) {
return -1;
}
return count;
}
};
```
</details>