• + 1 comment

    This is my elegant solution in java with 1-indexed lists:

    public static long arrayManipulation(int n, List<List<Integer>> queries) {
            // 1-indexed array
            List<Long> arr = new ArrayList<>() {
                {
                    add(Long.MIN_VALUE);
                    addAll(Collections.nCopies(n, 0L));
                }
            };
            for (List<Integer> query : queries) {
                Integer a = query.get(0);
                Integer b = query.get(1);
                Integer k = query.get(2);
                arr.set(a, arr.get(a) + k);
                if(b < n) {
                    arr.set(b + 1, arr.get(b + 1) - k);
                }
            }
            long answer = 0, sum = 0;
            for(int i = 1; i <= n; i++) {
                sum += arr.get(i);
                answer = Math.max(answer, sum);
            }
            return answer;
        }