Skip to content

Commit bde7b9e

Browse files
Merge pull request #3053 from benmak11/918
Create 0918-maximum-sum-circular-subarray.java
2 parents 2868340 + 776c41b commit bde7b9e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: java/0918-maximum-sum-circular-subarray.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int maxSubarraySumCircular(int[] nums) {
3+
int curMax = 0, curMin = 0;
4+
int globMax = nums[0], globMin = nums[0];
5+
int total = 0;
6+
for (int n : nums) {
7+
curMax = Math.max(curMax + n, n);
8+
curMin = Math.min(curMin + n, n);
9+
total += n;
10+
globMax = Math.max(curMax, globMax);
11+
globMin = Math.min(curMin, globMin);
12+
}
13+
return globMax > 0 ? Math.max(globMax, total - globMin) : globMax;
14+
}
15+
}

0 commit comments

Comments
 (0)