-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1094.car-pooling.cpp
More file actions
37 lines (34 loc) · 824 Bytes
/
1094.car-pooling.cpp
File metadata and controls
37 lines (34 loc) · 824 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
31
32
33
34
35
36
37
/*
* @lc app=leetcode id=1094 lang=cpp
*
* [1094] Car Pooling
*/
// @lc code=start
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
vector<pair<int, int>> people;
people.reserve(trips.size() * 2);
for(auto &trip : trips) {
people.push_back({trip[1], trip[0]});
people.push_back({trip[2], -trip[0]});
}
sort(people.begin(), people.end());
for(auto [_, people] : people) {
capacity -= people;
if(capacity < 0) return false;
}
return true;
}
};
// Accepted
// 58/58 cases passed (9 ms)
// Your runtime beats 78.76 % of cpp submissions
// Your memory usage beats 95.84 % of cpp submissions (9.8 MB)
// @lc code=end