Skip to content

Commit 803800c

Browse files
authored
Create car-pooling.cpp
1 parent 88510f8 commit 803800c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

C++/car-pooling.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
bool carPooling(vector<vector<int>>& trips, int capacity) {
7+
map<int, int> lookup;
8+
for (const auto& trip : trips) {
9+
lookup[trip[1]] += trip[0];
10+
lookup[trip[2]] -= trip[0];
11+
}
12+
for (const auto& [location, num] : lookup) {
13+
if ((capacity -= num) < 0) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
};

0 commit comments

Comments
 (0)