Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit20fede4

Browse files
add 918
1 parent34e5be8 commit20fede4

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+
packagecom.fishercoder.solutions;
2+
3+
publicclass_918 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* This is my original solution, but results in TLE on LeetCode.
7+
* Time: O(n^2)
8+
*/
9+
publicintmaxSubarraySumCircular(int[]nums) {
10+
int[]prefixSums;
11+
intmaxSum =Integer.MIN_VALUE;
12+
for (inti =0;i <nums.length;i++) {
13+
prefixSums =newint[nums.length];
14+
for (intj =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+
returnmaxSum;
24+
}
25+
}
26+
27+
publicstaticclassSolution2 {
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+
publicintmaxSubarraySumCircular(int[]nums) {
40+
intcurrMax =0;
41+
intglobalMax =nums[0];
42+
intcurrMin =0;
43+
intglobalMin =nums[0];
44+
inttotal =0;
45+
for (inti =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+
returnglobalMax >0 ?Math.max(globalMax,total -globalMin) :globalMax;
53+
}
54+
}
55+
56+
publicstaticclassSolution3 {
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+
publicintmaxSubarraySumCircular(int[]nums) {
68+
intcurrMax =nums[0];
69+
intglobalMax =nums[0];
70+
intcurrMin =nums[0];
71+
intglobalMin =nums[0];
72+
inttotal =nums[0];
73+
for (inti =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+
returnglobalMax >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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._918;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_918Test {
10+
privatestatic_918.Solution1solution1;
11+
privatestatic_918.Solution2solution2;
12+
privatestatic_918.Solution3solution3;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_918.Solution1();
17+
solution2 =new_918.Solution2();
18+
solution3 =new_918.Solution3();
19+
}
20+
21+
@Test
22+
publicvoidtest1() {
23+
assertEquals(3,solution1.maxSubarraySumCircular(newint[]{1, -2,3, -2}));
24+
}
25+
26+
@Test
27+
publicvoidtest2() {
28+
assertEquals(10,solution1.maxSubarraySumCircular(newint[]{5, -3,5}));
29+
}
30+
31+
@Test
32+
publicvoidtest3() {
33+
assertEquals(4,solution1.maxSubarraySumCircular(newint[]{3, -1,2, -1}));
34+
}
35+
36+
@Test
37+
publicvoidtest4() {
38+
assertEquals(3,solution1.maxSubarraySumCircular(newint[]{3, -2,2, -3}));
39+
}
40+
41+
@Test
42+
publicvoidtest5() {
43+
assertEquals(-1,solution1.maxSubarraySumCircular(newint[]{-2, -3, -1}));
44+
}
45+
46+
@Test
47+
publicvoidtest6() {
48+
assertEquals(10,solution1.maxSubarraySumCircular(newint[]{5, -3,5}));
49+
}
50+
51+
@Test
52+
publicvoidtest7() {
53+
assertEquals(10,solution2.maxSubarraySumCircular(newint[]{5, -3,5}));
54+
}
55+
56+
@Test
57+
publicvoidtest8() {
58+
assertEquals(10,solution3.maxSubarraySumCircular(newint[]{5, -3,5}));
59+
}
60+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp