Skip to content

Commit 5fda0be

Browse files
range addition
1 parent c782f61 commit 5fda0be

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

MEDIUM/src/medium/RangeAddition.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package medium;
2+
3+
public class RangeAddition {
4+
/**Previously AC'ed brute force solution results in TLE now.*/
5+
public static int[] getModifiedArray_TLE(int length, int[][] updates) {
6+
int[] nums = new int[length];
7+
int k = updates.length;
8+
for(int i = 0; i < k; i++){
9+
int start = updates[i][0];
10+
int end = updates[i][1];
11+
int inc = updates[i][2];
12+
for(int j = start; j <= end; j++){
13+
nums[j] += inc;
14+
}
15+
}
16+
return nums;
17+
}
18+
19+
/**Looked at this post: https://discuss.leetcode.com/topic/49691/java-o-k-n-time-complexity-solution and one OJ official article: https://leetcode.com/articles/range-addition/*/
20+
public static int[] getModifiedArray(int length, int[][] updates) {
21+
int[] nums = new int[length];
22+
int k = updates.length;
23+
for (int i = 0; i < k; i++){
24+
int start = updates[i][0];
25+
int end = updates[i][1];
26+
int inc = updates[i][2];
27+
nums[start] += inc;
28+
if (end < length-1) nums[end+1] -= inc;
29+
}
30+
31+
int sum = 0;
32+
for (int i = 0; i < length; i++){
33+
sum += nums[i];
34+
nums[i] = sum;
35+
}
36+
return nums;
37+
}
38+
39+
public static void main(String...args){
40+
/**5
41+
[[1,3,2],[2,4,3],[0,2,-2]]*/
42+
int length = 5;
43+
int[][] updates = new int[][]{
44+
{1,3,2},
45+
{2,4,3},
46+
{0,2,-2},
47+
};
48+
int[] result = getModifiedArray(length, updates);
49+
for (int i : result) {
50+
System.out.print(i + "\t");
51+
}
52+
}
53+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/)|[Solution](../../blob/master/MEDIUM/src/medium/LexicographicalNumbers.java)| O(n)|O(1) | Medium|
3232
|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[Solution](../../blob/master/MEDIUM/src/medium/DesignPhoneDirectory.java)| O(1)|O(n) | Medium|
3333
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../../blob/master/EASY/src/easy/GuessNumberHigherorLower.java)| O(logn)|O(1) | Easy| Binary Search
34+
|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../../blob/master/MEDIUM/src/medium/RangeAddition.java)| O(n+k)|O(1) | Medium|
3435
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
3536
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
3637
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|[Solution](../../blob/master/EASY/src/easy/NestedListWeightSum.java)| O(n)|O(h)) | Easy| DFS

0 commit comments

Comments
 (0)