Skip to content

Commit 481c3f4

Browse files
committed
Create 1094-car-pooling.cpp
1 parent 0b9c345 commit 481c3f4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: cpp/1094-car-pooling.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
bool carPooling(vector<vector<int>>& trips, int capacity) {
4+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;
5+
for (int i=0; i<trips.size(); ++i) {
6+
int numPassengers=trips[i][0], from=trips[i][1], to=trips[i][2];
7+
minHeap.push({from, numPassengers});
8+
minHeap.push({to, -numPassengers});
9+
}
10+
11+
int currCapacity = 0;
12+
while (!minHeap.empty()) {
13+
int currTime = minHeap.top().first;
14+
while (!minHeap.empty() and minHeap.top().first == currTime) {
15+
auto [time, numPassengers] = minHeap.top();
16+
minHeap.pop();
17+
currCapacity += numPassengers;
18+
}
19+
if (currCapacity > capacity)
20+
return false;
21+
}
22+
23+
return true;
24+
}
25+
};

0 commit comments

Comments
 (0)