-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy path1094.Car Pooling.cpp
More file actions
30 lines (30 loc) · 1001 Bytes
/
1094.Car Pooling.cpp
File metadata and controls
30 lines (30 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
vector<pair<int,int>> pickups;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> drops;
pickups.reserve(trips.size());
for (auto &t : trips){
pickups.emplace_back(t[1], t[0]);
drops.push({t[2], t[0]});
}
sort(pickups.begin(), pickups.end());
int i = 0, curr = 0;
while (!drops.empty()){
int nextDrop = drops.top().first;
while (i < (int)pickups.size() && pickups[i].first < nextDrop){
curr += pickups[i].second;
if (curr > capacity) return false;
++i;
}
curr -= drops.top().second;
drops.pop();
}
while (i < (int)pickups.size()){
curr += pickups[i].second;
if (curr > capacity) return false;
++i;
}
return true;
}
};