0918.cpp (508B)
1 class Solution { 2 public: 3 int maxSubarraySumCircular(vector<int> &nums) { 4 int total, maxSum, curMax, minSum, curMin; 5 6 total = curMax = curMin = 0; 7 maxSum = minSum = nums[0]; 8 for (int &n : nums) { 9 curMax = max(curMax + n, n); 10 maxSum = max(maxSum, curMax); 11 curMin = min(curMin + n, n); 12 minSum = min(minSum, curMin); 13 total += n; 14 } 15 return maxSum > 0 ? max(maxSum, total - minSum) : maxSum; 16 } 17 };