Short 13 lines solution in Java O(m+n)


  • 0
    S
    public class Solution {
        public int[] getModifiedArray(int length, int[][] updates) {
            int[] r = new int[length];
            for(int[] update : updates){
                r[update[0]] += update[2]; //add the number when operation start
                if(update[1] < length - 1)
                    r[update[1] + 1] -= update[2]; //minus the number when operation end
            }
            for(int i=0; i<r.length; i++) //adjust the array to get final result
                r[i] = i == 0 ? r[i] : r[i-1] + r[i]; 
            return r;
        }
    }

Log in to reply
 

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