Leek -- 918. Maximum Sum of Circular Subarrays

1. Topic:

Topic Link: 918. The Maximum Sum of Circular Subarrays - LeetCode

Second, the problem-solving steps:

The following is the process of solving this problem with the idea of ​​​​dynamic programming . I believe that everyone can understand and master this classic dynamic programming problem.

3. Reference code:

class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n=nums.size();
        vector<int> f(n);
        auto g=f;
        f[0]=nums[0];
        g[0]=nums[0];
        int fmax=f[0];
        int gmin=g[0];
        int sum=nums[0];
        for(int i=1;i<n;i++)
        {
            sum+=nums[i];
            f[i]=max(nums[i],f[i-1]+nums[i]);
            g[i]=min(nums[i],g[i-1]+nums[i]);
            fmax=max(fmax,f[i]);
            gmin=min(gmin,g[i]);
        }
        if(sum==gmin)
        {
            return fmax;
        }
        else
        {
            return max(fmax,sum-gmin);
        }
    }
};

The above is the whole process of analyzing this topic with the idea of ​​dynamic programming , have you learned it? If the above solutions are helpful to you, then please be careful and pay attention to it. We will continue to update the classic questions of dynamic programming in the future . See you in the next issue! ! ! ! ! ! ! ! !

Guess you like

Origin blog.csdn.net/weixin_70056514/article/details/131873972