Posted on

Description

Submission

class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int maxSum = nums[0], minSum = nums[0], total = 0, curMin = 0, curMax = 0;

        for(int num : nums) {
            curMax = max(curMax + num, num);
            maxSum = max(curMax, maxSum);
            curMin = min(curMin + num, num);
            minSum = min(curMin, minSum);
            total += num;
        }

        return maxSum > 0 ? max(maxSum, total - minSum) : maxSum;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *