原题题目
代码实现(首刷自解)
class Solution {
public:bool carPooling(vector<vector<int>>& trips, int capacity) {
sort(trips.begin(),trips.end(),[&](auto& a,auto& b){
return a[1] < b[1];});priority_queue<int,vector<int>,greater<int>> q;unordered_map<int,int> map;int nowcap = capacity;for(int i=0;i<trips.size();++i){
while(!q.empty()){
if(trips[i][1] >= q.top()){
capacity += map[q.top()];map.erase(map.find(q.top()));}else break;q.pop();}capacity -= trips[i][0];if(capacity < 0) return false;if(!map[trips[i][2]]) q.emplace(trips[i][2]);map[trips[i][2]] += trips[i][0]; }return true;}
};