forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeAddition.java
More file actions
28 lines (22 loc) · 604 Bytes
/
RangeAddition.java
File metadata and controls
28 lines (22 loc) · 604 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* https://leetcode.com/articles/range-addition/
*/
public class RangeAddition {
public int[] getModifiedArray(int length, int[][] updates) {
int[] res = new int[length];
for(int[] update : updates) {
int value = update[2];
int start = update[0];
int end = update[1];
res[start] += value;
if(end < length - 1)
res[end + 1] -= value;
}
int sum = 0;
for(int i = 0; i < length; i++) {
sum += res[i];
res[i] = sum;
}
return res;
}
}