forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1094-car-pooling.cpp
More file actions
25 lines (23 loc) · 867 Bytes
/
1094-car-pooling.cpp
File metadata and controls
25 lines (23 loc) · 867 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
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;
for (int i=0; i<trips.size(); ++i) {
int numPassengers=trips[i][0], from=trips[i][1], to=trips[i][2];
minHeap.push({from, numPassengers});
minHeap.push({to, -numPassengers});
}
int currCapacity = 0;
while (!minHeap.empty()) {
int currTime = minHeap.top().first;
while (!minHeap.empty() and minHeap.top().first == currTime) {
auto [time, numPassengers] = minHeap.top();
minHeap.pop();
currCapacity += numPassengers;
}
if (currCapacity > capacity)
return false;
}
return true;
}
};