-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1094.拼车.cpp
57 lines (48 loc) · 1.3 KB
/
1094.拼车.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
52
53
54
55
56
/*
* @lc app=leetcode.cn id=1094 lang=cpp
*
* [1094] 拼车
*/
// @lc code=start
class Difference {
private: vector<int> diff;
public:
Difference(vector<int>& nums) {
diff = vector<int> (nums.size());
diff[0] = nums[0];
for (int i = 1; i < nums.size(); i++)
diff[i] = nums[i] - nums[i - 1];
}
void increment(int i, int j, int val) {
diff[i] += val;
if (j + 1 < diff.size())
diff[j+1] -= val;
}
vector<int> result() {
vector<int> res(diff.size());
res[0] = diff[0];
for (int i = 1; i < diff.size(); i++)
res[i] = res[i-1] + diff[i];
return res;
}
};
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
vector<int> nums(1001);
Difference df(nums);
for (vector<int> trip: trips) {
int val = trip[0];
int i = trip[1];
// 这里需要注意乘客在 j 站之前就下车了,也就是区间是 i 到 j-1
int j = trip[2]-1;
df.increment(i, j, val);
}
vector<int> res = df.result();
for (int i = 0; i < res.size(); i++)
if (capacity < res[i])
return false;
return true;
}
};
// @lc code=end