so when total - minsum = 0 (circularMaxSum == 0), just return maxSum (noneCircularMaxSum)
class Solution {
public int maxSubarraySumCircular(int[] nums) {
int noneCircularMaxSum = kadane(nums);
int total = 0;
for (int i = 0; i < nums.length; i++) {
total += nums[i];
nums[i] = -nums[i];
}
int circularMaxSum = total + kadane(nums);
if (circularMaxSum == 0) {
return noneCircularMaxSum;
}
return Math.max(noneCircularMaxSum, circularMaxSum);
}
private int kadane(int[] nums) {
int max = Integer.MIN_VALUE;
int sum = 0;
for (int num : nums) {
sum += num;
max = Math.max(max, sum);
if (sum < 0) {
sum = 0;
}
}
return max;
}
}
/*
0. 1 2 3 4. 5
5 -3 5 5 -3 5
0 5 2 7 12 9 14
1,-2,3,-2 1,-2,3,-2
0 1 -1 2 0. 1.-1 2. 0
-3 -2 -3 -3 -2 -3
0 -3 -5 -8 -11 -13 -16
5 2 7 12 9 14
[-2,-3,-1, -2, -3, -1]
0 -2 -5 -6 -8 -11. -12
*/