14 liner O(N+k)


  • -1
    D
    public class Solution {
         public int[] getModifiedArray(int length, int[][] updates) {
             if(length ==0 || updates.length <1 || updates[0].length < 3){ return new int[]{0};}
           int result[] = new int[length];Arrays.fill(result,0);
           for(int i=0 ; i< updates.length; i++){
               int start= updates[i][0]; int end= updates[i][1];int value= updates[i][2];
               result[start] +=value; if(end < length-1)result[end+1] -= value;
           }
           for(int i =1 ; i< length ; i++){
               result[i] += result[i-1];
           }
           return result;
        }
    }
    

Log in to reply
 

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