1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public int maxSubarraySumCircular(int[] nums) { int maxSum = Integer.MIN_VALUE; int minSum = 0; int maxPre = 0, minPre = 0, sum = 0; for (int x : nums) { maxPre = Math.max(maxPre, 0) + x; maxSum = Math.max(maxSum, maxPre); minPre = Math.min(minPre, 0) + x; minSum = Math.min(minSum, minPre); sum += x; } return sum == minSum ? maxSum : Math.max(maxSum, sum - minSum); } }
|