C++ easy understanding O(n + k) solution


  • 0
    S
    class Solution {
    public:
        vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
            vector<int> res(length, 0);
            for(auto update : updates){
                int startIndex = update[0];
                int endIndex = update[1];
                int inc = update[2];
                res[startIndex] += inc;
                if(endIndex + 1 < length) res[endIndex + 1] -= inc;
            }
            for(int i = 1; i < length; i++){
                res[i] += res[i - 1];
            }
            return res;
        }
    };

Log in to reply
 

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