Skip to content

Commit d684943

Browse files
authored
Create Solution.java
1 parent 16aea1a commit d684943

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxSubarraySumCircular(int[] A) {
3+
int tot = 0;
4+
int curMax = 0;
5+
int maxSum = Integer.MIN_VALUE;
6+
int curMin = 0;
7+
int minSum = Integer.MAX_VALUE;
8+
for (int x : A) {
9+
tot += x;
10+
curMax = Math.max(curMax + x, x);
11+
maxSum = Math.max(maxSum, curMax);
12+
curMin = Math.min(curMin + x, x);
13+
minSum = Math.min(minSum, curMin);
14+
}
15+
return maxSum > 0 ? Math.max(maxSum, tot - minSum) : maxSum;
16+
}
17+
}

0 commit comments

Comments
 (0)