Leetcode 370 Solution

This article provides solution to leetcode question 370 (range-addition)

https://leetcode.com/problems/range-addition

Solution

class Solution {
public:
    vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
        vector<int> a(length + 1);

        for (auto update : updates)
        {
            a[update[0]] += update[2];
            a[update[1] + 1] -= update[2];
        }

        vector<int> res(length);
        int curr = 0;
        for (int i = 0; i < length; i++)
        {
            curr += a[i];
            res[i] = curr;
        }

        return res;
    }
};