MS 1186. Maximum Subarray Sum with One Deletion

class Solution {
public:
    int maximumSum(vector<int>& arr) {
        const int size = arr.size();
        if(size == 0){
            return 0;
        }
        if(size == 1){
            return arr[0];
        }
        vector<int> dp1(size, 0);//max subarray ending with delement i
        vector<int> dp2(size, 0);//max subarray ending with deletion of i
        dp1[0] = arr[0];
        dp2[0] = 0;
        int res = arr[0];
        for(int i = 1; i < size; i++){
            dp1[i] = max(dp1[i - 1] + arr[i], arr[i]);
            dp2[i] = max(dp2[i - 1] + arr[i], dp1[i - 1]);
            res = max(res, max(dp1[i], dp2[i]));
        }
        return res;
    }
};

Comments

Popular posts from this blog

算法的比较