1094. Car Pooling (M)
https://leetcode.com/problems/car-pooling/
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: falseInput: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: trueSolution:
Solution:Last updated
https://leetcode.com/problems/car-pooling/
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: falseInput: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: trueSolution:Last updated
boolean carPooling(int[][] trips, int capacity);trips = [[2,1,5],[3,3,7]], capacity = 40 <= trips[i][1] < trips[i][2] <= 1000boolean carPooling(int[][] trips, int capacity) {
// 最多有 1000 个车站
int[] nums = new int[1001];
// 构造差分解法
Difference df = new Difference(nums);
for (int[] trip : trips) {
// 乘客数量
int val = trip[0];
// 第 trip[1] 站乘客上车
int i = trip[1];
// 第 trip[2] 站乘客已经下车,
// 即乘客在车上的区间是 [trip[1], trip[2] - 1]
int j = trip[2] - 1;
// 进行区间操作
df.increment(i, j, val);
}
int[] res = df.result();
// 客车自始至终都不应该超载
for (int i = 0; i < res.length; i++) {
if (capacity < res[i]) {
return false;
}
}
return true;
}