Array Manipulation

  • + 0 comments

    Java

        public static long arrayManipulation(int n, List<List<Integer>> queries) {
            Map<Integer, Long> sum = new TreeMap<>(); // order by key
            for (List<Integer> list : queries) {
                int a = list.get(0);
                int b = list.get(1);
                long k = list.get(2);
                sum.put(a, sum.getOrDefault(a, 0L) + k);
                sum.put(b + 1, sum.getOrDefault(b + 1, 0L) - k);
            }
    
            long max = 0L;
            long current = 0L;
            for (long value : sum.values()) {
                current += value;
                max = Math.max(max, current);
            }
            return max;
    
        }