1186. Maximum Subarray Sum with One Deletion Medium
1/**
2 * [1186] Maximum Subarray Sum with One Deletion
3 *
4 * 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.
5 * Note that the subarray needs to be non-empty after deleting one element.
6 *
7 * Example 1:
8 *
9 * Input: arr = [1,-2,0,3]
10 * Output: 4
11 * Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.
12 * Example 2:
13 *
14 * Input: arr = [1,-2,-2,3]
15 * Output: 3
16 * Explanation: We just choose [3] and it's the maximum sum.
17 *
18 * Example 3:
19 *
20 * Input: arr = [-1,-1,-1,-1]
21 * Output: -1
22 * 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.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= arr.length <= 10^5
28 * -10^4 <= arr[i] <= 10^4
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/
34// discuss: https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn maximum_sum(arr: Vec<i32>) -> i32 {
40 0
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_1186() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.