My c++ solution


  • 0
    T

    I got my idea form the hints. It is said that we only need to record the startIndex and endIndex. So I create a vector which stores length + 1 elements to make sure it will not overflow.

    class Solution {
    public:
        vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
            vector<int> store(length + 1, 0);
            for(int i = 0; i < updates.size(); i++)
            {
                int start = updates[i][0]; 
                int end = updates[i][1];
                int inc = updates[i][2];
                store[start] = inc + store[start];//start to add more
                store[end + 1] = -inc + store[end + 1];// start to add less
            }
            int count = 0;
            for(int i = 0; i < store.size(); i++)
            {
                count = count + store[i];
                store[i] = count;
            }
            store.pop_back();
            return store;
        }
    };
    

Log in to reply
 

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