Leetcode 1094. 拼车 (差分数组)
作者:互联网
这种连续区间的加法处理,是典型的差分数组优化。
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
vector<int> delta(1010);
for(auto trip: trips){
delta[trip[1]] += trip[0];
delta[trip[2]] -= trip[0];
}
int cur = 0;
for(int i = 0; i <= 1000; i++){
cur += delta[i];
if(cur > capacity){
return false;
}
}
return true;
}
};
标签:1094,capacity,int,trip,拼车,vector,delta,return,Leetcode 来源: https://blog.csdn.net/wwxy1995/article/details/116568090