Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
- stations.push_back({target, 0});
- int len = stations.size(), steps = 0, prev = 0;
- long remain = startFuel;
- priority_queue<int> pq;
- for (int i = 0; i < len; ++i)
- {
- int curr = stations[i][0], fuel = stations[i][1];
- remain -= curr - prev;
- while (pq.size() && remain < 0)
- {
- remain += pq.top();
- pq.pop();
- ++steps;
- }
- if (remain < 0)return -1;
- pq.push(fuel);
- prev = curr;
- }
- return steps;
- }
- };
Add Comment
Please, Sign In to add comment