[leetcode] 5182. Maximum Subarray Sum with One Deletion

一、題意

Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible.

Note that the subarray needs to be non-empty after deleting one element.

 

Example 1:

Input: arr = [1,-2,0,3]
Output: 4
Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.

Example 2:

Input: arr = [1,-2,-2,3]
Output: 3
Explanation: We just choose [3] and it's the maximum sum.

Example 3:

Input: arr = [-1,-1,-1,-1]
Output: -1
Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.

 

Constraints:

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i] <= 10^4

二、題解 

題意:給出一串數字,返回和最大的非空子串,並且子串中最多可以刪除一個數字。

題解:類似於leetcode的53題。53題可以使用動態規劃的方法解出來,設dp[i]爲,以下標i爲結尾的子序列的最大和,那麼可以得到狀態轉移方程dp[i+1] = {dp[i]+nums[i+1] , dp[i]>=0; nums[i+1], dp[i]<0};本題可以採用類似的思想。設dp1[i]爲,以i下標爲結尾的子序列的最大和,dp2[i]爲,以i下標爲開始的子序列的最大和,那麼,最終的ans即爲 max(dp1[i-1] + dp2[i+1]),其中i爲arr中所有負數的下標。同時需要注意,當dp1[i-1]或者dp2[i+1]爲負數時,那麼可以不選擇這些數字,即將dp1[i-1]或dp2[i+1]賦爲0。

另外還要考慮arr全爲正,全爲負的特殊情況。

下面給出ac代碼:

class Solution {
public:
    int maximumSum(vector<int>& arr) {
        vector<int> dp1(arr.size());
        vector<int> dp2(arr.size());
        vector<int> negative;
        
        
        for(int i=0; i<arr.size(); i++){
            if(arr[i]<0){
                negative.push_back(i);
            }
            
            if(i==0){
                dp1[i] = arr[i];
            }else{
                if(dp1[i-1]>0){
                    dp1[i] = dp1[i-1] + arr[i];
                }else{
                    dp1[i] = arr[i];
                }
            }
        }
        
        
        for(int i=arr.size()-1; i>=0; i--){
            if(i==arr.size()-1){
                dp2[i] = arr[i];
            }else{
                if(dp2[i+1]>0){
                    dp2[i] = dp2[i+1]+arr[i];
                }else{
                    dp2[i] = arr[i];
                }
            }
        }
        
        int ans = -9999999;
        if(negative.size() == 0){
            return dp1[arr.size()-1];
        }
        if(negative.size() == arr.size()){
            for(int i=0; i<arr.size(); i++){
                if(i==0){
                    ans = arr[i];
                }else{
                    ans = max(ans, arr[i]);
                }
            }
            return ans;
        }
        
   
        
        for(int i=0; i<negative.size(); i++){
            int left,right;
            
            if(negative[i] == 0){
                left = 0;
            }else{
                left = dp1[negative[i]-1];
            }
            
            if(negative[i] == arr.size()-1){
                right = 0;
            }else{
                right = dp2[negative[i]+1];
            }
            
            if(left < 0){
                left = 0;
            }
            if(right < 0){
                right = 0;
            }
            
            ans = max(ans, left+right);
            
        }
         
        
        return ans;
        
         
        
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章