-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode1094.cpp
51 lines (47 loc) · 1.24 KB
/
leetcode1094.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*************************************************
Author: wenhaofang
Date: 2023-05-08
Description: leetcode1094 - Car Pooling
*************************************************/
#include <bits/stdc++.h>
using namespace std;
/**
* 方法一:差分数组
*
* 理论时间复杂度:O(n),其中 n 为数组大小
* 理论空间复杂度:O(n),其中 n 为数组大小
*/
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
vector<int> nums(1005);
vector<int> diff(1005);
for (vector<int> trip: trips) {
diff[trip[1]] += trip[0];
diff[trip[2]] -= trip[0];
}
nums[0] = diff[0];
for (int i = 1; i < nums.size(); i++) {
nums[i] = nums[i - 1] + diff[i];
}
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > capacity) {
return false;
}
}
return true;
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
vector<vector<int>> trips = {
{2, 1, 5},
{3, 3, 7}
};
int capacity = 3;
bool ans = solution -> carPooling(trips, capacity);
cout << ans << endl;
}