903. Range Addition

Code(Language:C++) (Judger:cloudjudge-cluster-4)
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

Popular posts from this blog

Amazon OA 763. Partition Labels

1427. Split Array into Fibonacci Sequence

05/25 周一