LeetCode每日一题(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

用 DP 来做, dp[i][deleted]代表从第 i 个元素开始向后的所有子数组加和的最大值, 如果 deleted 是 true 则代表在 i 之前已经删除过元素了, 所以这 dp[i][true]代表的是从 i 开始之后所有元素不进行删除操作的最大加和, dp[i][false]代表从 i 开始之后的所有元素最多做一次删除操作的加和。如果 arr[i]>=0, 则 dp[i][deleted] = max(arr[i] + dp[i+1][deleted], arr[i] + dp[i+1]), 其中这个 deleted 是当前 dp 的状态,因为当前的值>0, 所以无论后面的元素是否能做加和操作, 我们都可以加上当前值。 如果 arr[i] < 0, 则分成两种情况, 一个是之前已经做过删除操作了, 所以现在我们无法删掉 arr[i], 而且后面也无法做删除操作, dp[i][true] = arr[i] + dp[i+1][true]。如果之前没有做过删除操作, 则 dp[i][false] = max(arr[i] + dp[i][false], dp[i][true]), 也就是要么我删掉当前值,然后取后面的不做删除操作的最大值, 或者我不删掉当前值, 但是取后面做删除操作的最大值, 取两种情况较大的那个。


use std::collections::HashMap;

impl Solution {
    fn dp(arr: &Vec<i32>, i: usize, deleted: bool, cache: &mut HashMap<(usize, bool), i32>) -> i32 {
        let curr = arr[i];
        if i == arr.len() - 1 {
            return curr;
        }
        if curr >= 0 {
            let next = if let Some(c) = cache.get(&(i + 1, deleted)) {
                *c
            } else {
                Solution::dp(arr, i + 1, deleted, cache)
            };
            let ans = if next < 0 { curr } else { curr + next };
            cache.insert((i, deleted), ans);
            return ans;
        }
        if deleted {
            let next = if let Some(c) = cache.get(&(i + 1, deleted)) {
                *c
            } else {
                Solution::dp(arr, i + 1, deleted, cache)
            };
            cache.insert((i, deleted), curr + next);
            return curr + next;
        }
        let delete = if let Some(c) = cache.get(&(i + 1, true)) {
            *c
        } else {
            Solution::dp(arr, i + 1, true, cache)
        };
        let keep = if let Some(c) = cache.get(&(i + 1, false)) {
            *c
        } else {
            Solution::dp(arr, i + 1, false, cache)
        };
        let ans = delete.max(curr + keep);
        cache.insert((i, deleted), ans);
        ans
    }
    pub fn maximum_sum(arr: Vec<i32>) -> i32 {
        let mut ans = i32::MIN;
        let mut cache = HashMap::new();
        for i in 0..arr.len() {
            ans = ans.max(Solution::dp(&arr, i, false, &mut cache));
        }
        ans
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值