Skip to content

Commit 2f5a7f9

Browse files
[N-0] add 689
1 parent ab0e028 commit 2f5a7f9

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Your ideas/fixes/algorithms are more than welcome!
3636
|693|[Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_693.java) | O(n) | O(1) | Easy |
3737
|692|[Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_692.java) | O(nlogk) | O(n) | Medium |
3838
|690|[Employee Importance](https://leetcode.com/problems/employee-importance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_690.java) | O(n) | O(h) | Easy | DFS
39+
|689|[Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_689.java) | O(n) | O(n) | Hard | DP
3940
|688|[Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_688.java) | O(n^2) | O(n^2) | Medium | DP
4041
|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_687.java) | O(n) | O(h) | Easy | DFS
4142
|686|[Repeated String Match](https://leetcode.com/problems/repeated-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_686.java) | O(n*(m+n)) | O(m+n) | Easy |

src/main/java/com/fishercoder/solutions/_689.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,64 @@
2121
*/
2222
public class _689 {
2323
public static class Solution1 {
24+
/**we basically need to find the interval (i, i+k-1) as the middle interval, where k <= i <= n-2k
25+
* then this interval (0, i-1) will be the left interval
26+
* the interval (i+k, n-1) will be the right interval.
27+
*
28+
* Please pay special attention to the variable name I use here: this `k` is not a random one, it's the `k`
29+
* from the passed in parameter.
30+
*
31+
* Credit: https://discuss.leetcode.com/topic/105577/c-java-dp-with-explanation-o-n/*/
2432
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
33+
if (nums == null || nums.length == 0) {
34+
return new int[]{};
35+
}
36+
int n = nums.length;
37+
int[] sums = new int[n + 1];
38+
for (int i = 0; i < n; i++) {
39+
sums[i + 1] = sums[i] + nums[i];
40+
}
2541

26-
return new int[]{};
42+
int[] leftMax = new int[n];
43+
for (int i = k, total = sums[k] - sums[0]; i < n; i++) {
44+
if (sums[i + 1] - sums[i + 1 - k] > total) {
45+
leftMax[i] = i + 1 - k;
46+
total = sums[i + 1] - sums[i + 1 - k];
47+
} else {
48+
leftMax[i] = leftMax[i - 1];
49+
}
50+
}
51+
52+
int[] rightMax = new int[n];
53+
rightMax[n - k] = n - k;
54+
for (int i = n - k - 1, total = sums[n] - sums[n - k]; i >= 0; i--) {
55+
if (sums[i + k] - sums[i] >= total) {
56+
rightMax[i] = i;
57+
total = sums[i + k] - sums[i];
58+
} else {
59+
rightMax[i] = rightMax[i + 1];
60+
}
61+
}
62+
63+
//try to find all possible middle intervals
64+
int[] result = new int[3];
65+
int max = 0;
66+
for (int i = k; i <= n - 2 * k; i++) {
67+
int left = leftMax[i - 1];
68+
int right = rightMax[i + k];
69+
int total = (sums[i + k] - sums[i]) + (sums[left + k] - sums[left]) + (sums[right + k] - sums[right]);
70+
if (total > max) {
71+
max = total;
72+
result[0] = left;
73+
result[1] = i;
74+
result[2] = right;
75+
}
76+
}
77+
return result;
2778
}
2879
}
80+
81+
public static class Solution2 {
82+
/**reference: https://leetcode.com/articles/maximum-sum-of-3-non-overlapping-intervals*/
83+
}
2984
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._689;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _689Test {
10+
private static _689.Solution1 solution1;
11+
private static int[] nums;
12+
private static int[] expected;
13+
private static int k;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _689.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
nums = new int[]{1, 2, 1, 2, 6, 7, 5, 1};
23+
expected = new int[]{0, 3, 5};
24+
k = 2;
25+
assertArrayEquals(expected, solution1.maxSumOfThreeSubarrays(nums, 2));
26+
}
27+
}

0 commit comments

Comments
 (0)