1186. 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 <= 105
  • -104 <= arr[i] <= 104

Rust Solution

struct Solution;

impl Solution {
    fn maximum_sum(arr: Vec<i32>) -> i32 {
        let n = arr.len();
        let mut left_sum = 0;
        let mut left_max = vec![0; n];
        let mut left_min = 0;
        let mut right_sum = 0;
        let mut right_max = vec![0; n];
        let mut right_min = 0;
        for i in 0..n {
            left_sum += arr[i];
            left_max[i] = left_sum - left_min;
            left_min = left_min.min(left_sum);
        }
        for i in (0..n).rev() {
            right_sum += arr[i];
            right_max[i] = right_sum - right_min;
            right_min = right_min.min(right_sum);
        }
        let mut res = std::i32::MIN;
        for i in 0..n {
            res = res.max(left_max[i]);
            res = res.max(right_max[i]);
            if i > 0 && i + 1 < n {
                res = res.max(left_max[i - 1] + right_max[i + 1]);
            }
        }
        res
    }
}

#[test]
fn test() {
    let arr = vec![1, -2, 0, 3];
    let res = 4;
    assert_eq!(Solution::maximum_sum(arr), res);
    let arr = vec![1, -2, -2, 3];
    let res = 3;
    assert_eq!(Solution::maximum_sum(arr), res);
    let arr = vec![-1, -1, -1, -1];
    let res = -1;
    assert_eq!(Solution::maximum_sum(arr), res);
    let arr = vec![0, -5, -6, 5, 0, -5];
    let res = 5;
    assert_eq!(Solution::maximum_sum(arr), res);
}

Having problems with this solution? Click here to submit an issue on github.