903. Range Addition
class Solution {
public:
/**
* @param length: the length of the array
* @param updates: update operations
* @return: the modified array after all k operations were executed
*/
vector<int> getModifiedArray(int length, vector<vector<int>> &updates) {
// Write your code here
vector<int> nums(length + 1, 0);
for(int i = 0; i < updates.size(); i++){
nums[updates[i][0]] += updates[i][2];
nums[updates[i][1] + 1] -= updates[i][2];
}
int preSum = 0;
vector<int> res;
for(int i = 0; i < length; i++){
preSum += nums[i];
res.push_back(preSum);
}
return res;
}
};
Comments
Post a Comment