-
Notifications
You must be signed in to change notification settings - Fork 0
Range Addition
Tim_Gao edited this page Sep 18, 2016
·
1 revision
- Mistake: we need to deduct operation[2] at position operation[1]+1, not operation[1]
public class Solution {
public int[] getModifiedArray(int length, int[][] updates) {
int[] arr = new int[length];
if(updates.length == 0){
return arr;
}
for (int[] operation : updates){
arr[operation[0]] += operation[2];
if (operation[1] + 1 < length)
arr[operation[1]+1] -= operation[2];
}
int sum = 0;
for (int i=0; i<arr.length; ++i){
sum += arr[i];
arr[i] = sum;
}
return arr;
}
}