Leetcode 871
Leetcode 871 Minimum numbers of Refueliing Stops
一辆汽车要从起点开到终点,途中有几个加油站可以加油,但是到每个加油站需要一定路程的油。问到达终点最少需要几次加油。
解答
优先队列
class Solution { public: int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) { int len = stations.size(); if(!stations.empty() && startFuel<stations[0][0]) return -1; priority_queue<int> q; int fuel = startFuel; int ans=0; cout<<"ok"<<endl; for(int i=0;i<stations.size();i++){ q.push(stations[i][1]); while(i+1<stations.size() && fuel<stations[i+1][0] && !q.empty()){ fuel+=q.top();q.pop();ans++; } if(i+1<stations.size() && fuel<stations[i+1][0]) return -1; } while(fuel<target && !q.empty()){ fuel+=q.top();q.pop(); ans++; } if(fuel<target) return -1; return ans; } };