LC 918. Maximum Sum Circular Subarray

link

LC 918. Maximum Sum Circular Subarray

 

 

class Solution {
public:
    int maxSubarraySumCircular(vector<int>& A) {
        int maxsum=INT_MIN;
        int minsum=INT_MAX;
        int total=0;
        int curmax=0;
        int curmin=0;
        for(int i=0;i<A.size();i++){
            curmax=max(curmax+A[i],A[i]);
            curmin=min(curmin+A[i],A[i]);
            maxsum=max(maxsum,curmax);
            minsum=min(minsum,curmin);
            total+=A[i];
        }
        return total==minsum?maxsum : max(maxsum,total-minsum);
    }
};

 

上一篇:力扣1423. 可获得的最大点数-C语言


下一篇:每日随机一题 leetcode638. 大礼包