Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
  4. stations.push_back({target, 0});
  5. int len = stations.size(), steps = 0, prev = 0;
  6. long remain = startFuel;
  7. priority_queue<int> pq;
  8. for (int i = 0; i < len; ++i)
  9. {
  10. int curr = stations[i][0], fuel = stations[i][1];
  11. remain -= curr - prev;
  12. while (pq.size() && remain < 0)
  13. {
  14. remain += pq.top();
  15. pq.pop();
  16. ++steps;
  17. }
  18.  
  19. if (remain < 0)return -1;
  20. pq.push(fuel);
  21. prev = curr;
  22. }
  23. return steps;
  24. }
  25. };
Add Comment
Please, Sign In to add comment