3ms Java O(n+k) time O(1) space Solution


  • 3
    X
    public class Solution {
        public int[] getModifiedArray(int length, int[][] updates) {
            int[] nums = new int[length];
            for (int[] update : updates) {
                nums[update[1]] += update[2];
                if (update[0] > 0) nums[update[0] - 1] -= update[2];
            }
            for (int i = length - 2; i >= 0; i--) {
                nums[i] = nums[i + 1] + nums[i];
            }
            return nums;
        }
    }

Log in to reply
 

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