Skip to content

Commit a7c1b09

Browse files
[N-0] refactor 370
1 parent cc0e676 commit a7c1b09

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Your ideas/fixes/algorithms are more than welcome!
295295
|373|[Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_373.java)| O(klogk)|O(k) | Medium| Heap
296296
|372|[Super Pow](https://leetcode.com/problems/super-pow/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_372.java)| O(n)|O(1) | Medium| Math
297297
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_371.java)| O(n)|O(1) | Easy|
298-
|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_370.java)| O(n+k)|O(1) | Medium|
298+
|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_370.java)| O(n+k)|O(1) | Medium|Array
299299
|369|[Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_369.java)| O(n)|O(1) | Medium| Linked List
300300
|368|[Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_368.java)| O(n^2)|O(n) | Medium| DP
301301
|367|[Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_367.java)| O(n)|O(1) | Medium|
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.fishercoder.solutions;
22

3-
/**Assume you have an array of length n initialized with all 0's and are given k update operations.
4-
5-
Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc.
6-
7-
Return the modified array after all k operations were executed.
3+
/**
4+
* 370. Range Addition
5+
*
6+
* Assume you have an array of length n initialized with all 0's and are given k update operations.
7+
* Each operation is represented as a triplet: [startIndex, endIndex, inc]
8+
* which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc.
9+
* Return the modified array after all k operations were executed.
810
911
Example:
1012
@@ -33,65 +35,35 @@
3335
3436
After applying operation [0, 2, -2]:
3537
[-2, 0, 3, 5, 3 ]
36-
Hint:
3738
39+
Hint:
3840
Thinking of using advanced data structures? You are thinking it too complicated.
3941
For each update operation, do you really need to update all elements between i and j?
4042
Update only the first and end element is sufficient.
41-
The optimal time complexity is O(k + n) and uses O(1) extra space.*/
43+
The optimal time complexity is O(k + n) and uses O(1) extra space.
44+
*/
4245

4346
public class _370 {
44-
/**Previously AC'ed brute force solution results in TLE now.*/
45-
public static int[] getModifiedArray_TLE(int length, int[][] updates) {
46-
int[] nums = new int[length];
47-
int k = updates.length;
48-
for (int i = 0; i < k; i++) {
49-
int start = updates[i][0];
50-
int end = updates[i][1];
51-
int inc = updates[i][2];
52-
for (int j = start; j <= end; j++) {
53-
nums[j] += inc;
47+
public static class Solution1 {
48+
public int[] getModifiedArray(int length, int[][] updates) {
49+
int[] nums = new int[length];
50+
int k = updates.length;
51+
for (int i = 0; i < k; i++) {
52+
int start = updates[i][0];
53+
int end = updates[i][1];
54+
int inc = updates[i][2];
55+
nums[start] += inc;
56+
if (end < length - 1) {
57+
nums[end + 1] -= inc;
58+
}
5459
}
55-
}
56-
return nums;
57-
}
5860

59-
/**
60-
* 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/
61-
*/
62-
public static int[] getModifiedArray(int length, int[][] updates) {
63-
int[] nums = new int[length];
64-
int k = updates.length;
65-
for (int i = 0; i < k; i++) {
66-
int start = updates[i][0];
67-
int end = updates[i][1];
68-
int inc = updates[i][2];
69-
nums[start] += inc;
70-
if (end < length - 1) {
71-
nums[end + 1] -= inc;
61+
int sum = 0;
62+
for (int i = 0; i < length; i++) {
63+
sum += nums[i];
64+
nums[i] = sum;
7265
}
73-
}
74-
75-
int sum = 0;
76-
for (int i = 0; i < length; i++) {
77-
sum += nums[i];
78-
nums[i] = sum;
79-
}
80-
return nums;
81-
}
82-
83-
public static void main(String... args) {
84-
/**5
85-
[[1,3,2],[2,4,3],[0,2,-2]]*/
86-
int length = 5;
87-
int[][] updates = new int[][]{
88-
{1, 3, 2},
89-
{2, 4, 3},
90-
{0, 2, -2},
91-
};
92-
int[] result = getModifiedArray(length, updates);
93-
for (int i : result) {
94-
System.out.print(i + "\t");
66+
return nums;
9567
}
9668
}
9769
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._370;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _370Test {
10+
private static _370.Solution1 solution1;
11+
private static int[][] updates;
12+
private static int length;
13+
private static int[] expected;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _370.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
updates = new int[][]{
23+
{1, 3, 2},
24+
{2, 4, 3},
25+
{0, 2, -2},
26+
};
27+
length = 5;
28+
expected = new int[]{-2, 0, 3, 5, 3};
29+
assertArrayEquals(expected, solution1.getModifiedArray(length, updates));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)