Skip to content

Commit 20fede4

Browse files
add 918
1 parent 34e5be8 commit 20fede4

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ _If you like this project, please leave me a star._ ★
480480
|923|[3Sum With Multiplicity](https://leetcode.com/problems/3sum-with-multiplicity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_923.java) | |Medium|Two Pointers
481481
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | |Easy|
482482
|921|[Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_921.java) | |Medium|Stack, Greedy
483+
|918|[Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_918.java) | |Medium|Array, DP, Monotonic Queue
483484
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | |Easy|
484485
|914|[X of a Kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_914.java) | |Easy|
485486
|912|[Sort an Array](https://leetcode.com/problems/sort-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_912.java) | |Easy|
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _918 {
4+
public static class Solution1 {
5+
/**
6+
* This is my original solution, but results in TLE on LeetCode.
7+
* Time: O(n^2)
8+
*/
9+
public int maxSubarraySumCircular(int[] nums) {
10+
int[] prefixSums;
11+
int maxSum = Integer.MIN_VALUE;
12+
for (int i = 0; i < nums.length; i++) {
13+
prefixSums = new int[nums.length];
14+
for (int j = i, k = 0; j < (i + nums.length); j++) {
15+
if (k == 0) {
16+
prefixSums[k] = nums[(j + nums.length) % nums.length];
17+
} else {
18+
prefixSums[k] = prefixSums[k - 1] + nums[(j + nums.length) % nums.length];
19+
}
20+
maxSum = Math.max(maxSum, prefixSums[k++]);
21+
}
22+
}
23+
return maxSum;
24+
}
25+
}
26+
27+
public static class Solution2 {
28+
/**
29+
* Credit: https://leetcode.com/problems/maximum-sum-circular-subarray/discuss/178422/One-Pass
30+
* Think of two cases:
31+
* 1. the max comes from the contiguous part of the original array
32+
* 2. the max comes from connecting the tail part and the head part of the original array.
33+
* See graph from the above link.
34+
* <p>
35+
* Time: O(n)
36+
* <p>
37+
* This is a follow-up from https://leetcode.com/problems/maximum-subarray/ which is solved by Kadane's algorithm.
38+
*/
39+
public int maxSubarraySumCircular(int[] nums) {
40+
int currMax = 0;
41+
int globalMax = nums[0];
42+
int currMin = 0;
43+
int globalMin = nums[0];
44+
int total = 0;
45+
for (int i = 0; i < nums.length; i++) {
46+
currMax = Math.max(nums[i], currMax + nums[i]);
47+
globalMax = Math.max(globalMax, currMax);
48+
currMin = Math.min(currMin + nums[i], nums[i]);
49+
globalMin = Math.min(currMin, globalMin);
50+
total += nums[i];
51+
}
52+
return globalMax > 0 ? Math.max(globalMax, total - globalMin) : globalMax;
53+
}
54+
}
55+
56+
public static class Solution3 {
57+
/**
58+
* Credit: https://leetcode.com/problems/maximum-sum-circular-subarray/discuss/633058/Java-or-C%2B%2B-or-Python3-or-With-detailed-explanation-or-O(N)-time-or-O(1)
59+
* This one is similar to the above Solution2, but only slightly differs in that it starts from i = 1 instead of i = 0
60+
* And it listed out a few examples to help illustrate why this algorithm makes sense.
61+
* Which I think is easier to make sense of.
62+
* <p>
63+
* Time: O(n)
64+
* <p>
65+
* This is a follow-up from https://leetcode.com/problems/maximum-subarray/ which is solved by Kadane's algorithm.
66+
*/
67+
public int maxSubarraySumCircular(int[] nums) {
68+
int currMax = nums[0];
69+
int globalMax = nums[0];
70+
int currMin = nums[0];
71+
int globalMin = nums[0];
72+
int total = nums[0];
73+
for (int i = 1; i < nums.length; i++) {
74+
currMax = Math.max(nums[i], currMax + nums[i]);
75+
globalMax = Math.max(globalMax, currMax);
76+
currMin = Math.min(currMin + nums[i], nums[i]);
77+
globalMin = Math.min(currMin, globalMin);
78+
total += nums[i];
79+
}
80+
return globalMax > 0 ? Math.max(globalMax, total - globalMin) : globalMax;
81+
}
82+
}
83+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._918;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _918Test {
10+
private static _918.Solution1 solution1;
11+
private static _918.Solution2 solution2;
12+
private static _918.Solution3 solution3;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _918.Solution1();
17+
solution2 = new _918.Solution2();
18+
solution3 = new _918.Solution3();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
assertEquals(3, solution1.maxSubarraySumCircular(new int[]{1, -2, 3, -2}));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertEquals(10, solution1.maxSubarraySumCircular(new int[]{5, -3, 5}));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(4, solution1.maxSubarraySumCircular(new int[]{3, -1, 2, -1}));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
assertEquals(3, solution1.maxSubarraySumCircular(new int[]{3, -2, 2, -3}));
39+
}
40+
41+
@Test
42+
public void test5() {
43+
assertEquals(-1, solution1.maxSubarraySumCircular(new int[]{-2, -3, -1}));
44+
}
45+
46+
@Test
47+
public void test6() {
48+
assertEquals(10, solution1.maxSubarraySumCircular(new int[]{5, -3, 5}));
49+
}
50+
51+
@Test
52+
public void test7() {
53+
assertEquals(10, solution2.maxSubarraySumCircular(new int[]{5, -3, 5}));
54+
}
55+
56+
@Test
57+
public void test8() {
58+
assertEquals(10, solution3.maxSubarraySumCircular(new int[]{5, -3, 5}));
59+
}
60+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy