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:
1 | Input: arr = [1,-2,0,3] |
Example 2:
1 | Input: arr = [1,-2,-2,3] |
Example 3:
1 | Input: arr = [-1,-1,-1,-1] |
Constraints:
1 <= arr.length <= 10^5
-10^4 <= arr[i] <= 10^4
分析
$dp[i][0]$ 表示没有删除元素且以 $a[i]$ 结尾的最大和的子数组的和。$dp[i][0]$ 表示删除了一个元素且以 $a[i]$($a[i]$ 也有可能被删除)结尾的最大和的子数组的和。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/29/leetcode-1186/
License: 知识共享署名-非商业性使用 4.0 国际许可协议