Array Manipulation

  • + 0 comments

    java:

        public static long arrayManipulation(int n, List<List<Integer>> queries) {
        // Write your code here
            long[] tmp = new long[n];        
            long max=0,tmpSum=0;
            int a, b, k;
            for (List<Integer> query : queries) {
                a = query.get(0);
                b = query.get(1);
                k = query.get(2);
    
                tmp[a-1] += k;
                 if (b < n) {
                    tmp[b] -= k;
                }
            }
            
            for (int i = 0; i < n; i++) {
                tmpSum += tmp[i];
                max = Math.max(max, tmpSum);
            }
            return max;
    
        }