##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1186.html

# 1186. Maximum Subarray Sum with One Deletion (Medium)

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

Related Topics:
Dynamic Programming

## Solution 1. DP

Let:

• dp[i][0] be the maximum sum of subarrays that end at A[i] and don’t without deletion.
• dp[i][1] be the maximum sum of subarrays that end at A[i] and have one deletion.

For dp[i][0], we have two options:

1. Only pick A[i]A[i].
2. Append A[i] to previous best result without deletion – dp[i - 1][0] + A[i] So dp[i][0] = max(A[i], dp[i - 1][0] + A[i]).

For dp[i][1], we have two options:

1. treat A[i] as deleted and use the previous best result without deletion – dp[i - 1][0].
2. Append A[i] to the previous best result with deletion – dp[i - 1][1] + A[i].

So in sum:

dp[i][0] = max(A[i], dp[i - 1][0] + A[i])
dp[i][1] = max(dp[i - 1][0], dp[i - 1][1] + A[i])
where i >= 1

dp[0][0] = A[0]
dp[0][1] = -INF


Note that in implementation dp[0][0] can be set as 0 since it’s only used in A[1] + dp[0][1] and thus setting it to 0 safely ignores it.

// OJ: https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/
// Time: O(N)
// Space: O(N)
class Solution {
public:
int maximumSum(vector<int>& A) {
int N = A.size(), ans = A[0];
auto dp = vector<vector<int>>(N, vector<int>(2));
dp[0][0] = A[0];
for (int i = 1; i < N; ++i) {
dp[i][0] = max(A[i], dp[i - 1][0] + A[i]);
dp[i][1] = max(A[i] + dp[i - 1][1], dp[i - 1][0]);
ans = max({ ans, dp[i][0], dp[i][1] });
}
return ans;
}
};


## Solution 2. DP with Space Optimization

Since dp[i][x] is only dependent on dp[i - 1][y], we can reduce the dp array from N * 2 to 2 * 2.

// OJ: https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/
// Time: O(N)
// Space: O(1)
class Solution {
public:
int maximumSum(vector<int>& A) {
int N = A.size(), ans = A[0];
int pick = A[0], skip = 0;
for (int i = 1; i < N; ++i) {
int p = max(A[i], pick + A[i]), s = max(A[i] + skip, pick);
pick = p;
skip = s;
ans = max({ ans, p, s });
}
return ans;
}
};


## Solution 3.

// OJ: https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/
// Time: O(N)
// Space: O(N)
// Ref: https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/discuss/377397/Intuitive-Java-Solution-With-Explanation
class Solution {
public:
int maximumSum(vector<int>& A) {
int N = A.size(), ans = A[0];
vector<int> maxEndHere(N), maxStartHere(N);
maxEndHere[0] = A[0];
for (int i = 1; i < N; ++i) {
maxEndHere[i] = max(A[i], maxEndHere[i - 1] + A[i]);
ans = max(ans, maxEndHere[i]);
}
maxStartHere[N - 1] = A[N - 1];
for (int i = N - 2; i >= 0; --i) maxStartHere[i] = max(A[i], maxStartHere[i + 1] + A[i]);
for (int i = 1; i < N - 1; ++i) ans = max(ans, maxEndHere[i - 1] + maxStartHere[i + 1]);
return ans;
}
};

• class Solution {
public int maximumSum(int[] arr) {
int length = arr.length;
int max1 = arr[0];
int max2 = arr[0];
int max3 = arr[0];
int max = arr[0];
for (int i = 1; i < length; i++) {
int newMax1 = Math.max(max1 + arr[i], arr[i]);
int newMax2 = Math.max(Math.max(max2, max3) + arr[i], arr[i]);
int newMax3 = max1;
max1 = newMax1;
max2 = newMax2;
max3 = newMax3;
max = Math.max(max, Math.max(Math.max(max1, max2), max3));
}
return max;
}
}

############

class Solution {
public int maximumSum(int[] arr) {
int n = arr.length;
int[] left = new int[n];
int[] right = new int[n];
int t = 0;
for (int i = 0; i < n; ++i) {
t = Math.max(t, 0) + arr[i];
left[i] = t;
}
t = 0;
for (int i = n - 1; i >= 0; --i) {
t = Math.max(t, 0) + arr[i];
right[i] = t;
}
int ans = Arrays.stream(left).max().getAsInt();
for (int i = 1; i < n - 1; ++i) {
ans = Math.max(ans, left[i - 1] + right[i + 1]);
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/
// Time: O(N)
// Space: O(N)
class Solution {
public:
int maximumSum(vector<int>& A) {
int N = A.size(), ans = A[0];
auto dp = vector<vector<int>>(N, vector<int>(2));
dp[0][0] = A[0];
for (int i = 1; i < N; ++i) {
dp[i][0] = max(A[i], dp[i - 1][0] + A[i]);
dp[i][1] = max(A[i] + dp[i - 1][1], dp[i - 1][0]);
ans = max({ ans, dp[i][0], dp[i][1] });
}
return ans;
}
};

• class Solution:
def maximumSum(self, arr: List[int]) -> int:
n = len(arr)
left = [0] * n
right = [0] * n
t = 0
for i, v in enumerate(arr):
t = max(t, 0) + v
left[i] = t
t = 0
for i in range(n - 1, -1, -1):
t = max(t, 0) + arr[i]
right[i] = t
ans = max(left)
for i in range(1, n - 1):
ans = max(ans, left[i - 1] + right[i + 1])
return ans

############

# 1186. Maximum Subarray Sum with One Deletion
# https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/

class Solution:
def maximumSum(self, arr: List[int]) -> int:
n = len(arr)

dp1 = [arr[0] for _ in range(n)]
dp2 = [arr[0] for _ in range(n)]

for i in range(1, n):
dp1[i] = max(dp1[i-1] + arr[i], arr[i])
dp2[i] = max(dp2[i-1] + arr[i], arr[i])

if i >= 2:
dp1[i] = max(dp1[i], dp2[i - 2] + arr[i])

return max(dp1)


• func maximumSum(arr []int) int {
n := len(arr)
left := make([]int, n)
right := make([]int, n)
t := 0
ans := math.MinInt32
for i, v := range arr {
t = max(t, 0) + v
left[i] = t
ans = max(ans, left[i])
}
t = 0
for i := n - 1; i >= 0; i-- {
t = max(t, 0) + arr[i]
right[i] = t
}
for i := 1; i < n-1; i++ {
ans = max(ans, left[i-1]+right[i+1])
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

• function maximumSum(arr: number[]): number {
const n = arr.length;
const left: number[] = new Array(n);
const right: number[] = new Array(n);
for (let i = 0, s = 0; i < n; ++i) {
s = Math.max(s, 0) + arr[i];
left[i] = s;
}
for (let i = n - 1, s = 0; i >= 0; --i) {
s = Math.max(s, 0) + arr[i];
right[i] = s;
}
let ans = Math.max(...left);
for (let i = 1; i < n - 1; ++i) {
ans = Math.max(ans, left[i - 1] + right[i + 1]);
}
return ans;
}