C++ O(n + k) Time O(1) Extra space 6-line Short and Elegant Solution


  • 1
    F
    vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
        vector<int> res(length + 1);
        for (auto & u : updates)
            res[u[0]] += u[2], res[u[1] + 1] += -u[2];
        for (int i = 1; i < res.size(); ++i)
            res[i] += res[i - 1];
        return res.pop_back(), res;
    }
    

    The code shouldn't be too hard to understand, leave comments if you have questions.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.