1186. Maximum Subarray Sum with One Deletion LeetCode Solution
In this guide, you will get 1186. Maximum Subarray Sum with One Deletion LeetCode Solution with the best time and space complexity. The solution to Maximum Subarray Sum with One Deletion problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Subarray Sum with One Deletion solution in C++
- Maximum Subarray Sum with One Deletion solution in Java
- Maximum Subarray Sum with One Deletion solution in Python
- Additional Resources
Problem Statement of 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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
1186. Maximum Subarray Sum with One Deletion LeetCode Solution in C++
class Solution {
public:
// Similar to 53. Maximum Subarray
int maximumSum(vector<int>& arr) {
// dp[0][i] := the maximum sum subarray ending in i (no deletion)
// dp[1][i] := the maximum sum subarray ending in i (at most 1 deletion)
vector<vector<int>> dp(2, vector<int>(arr.size()));
dp[0][0] = arr[0];
dp[1][0] = arr[0];
for (int i = 1; i < arr.size(); ++i) {
dp[0][i] = max(arr[i], dp[0][i - 1] + arr[i]);
dp[1][i] =
max({arr[i], dp[1][i - 1] + arr[i], dp[0][i - 1] /*delete arr[i]*/});
}
return ranges::max(dp[1]);
}
};
/* code provided by PROGIEZ */
1186. Maximum Subarray Sum with One Deletion LeetCode Solution in Java
class Solution {
// Similar to 53. Maximum Subarray
public int maximumSum(int[] arr) {
// dp[0][i] := the maximum sum subarray ending in i (no deletion)
// dp[1][i] := the maximum sum subarray ending in i (at most 1 deletion)
int[][] dp = new int[2][arr.length];
dp[0][0] = arr[0];
dp[1][0] = arr[0];
for (int i = 1; i < arr.length; ++i) {
dp[0][i] = Math.max(arr[i], dp[0][i - 1] + arr[i]);
dp[1][i] = Math.max(arr[i], Math.max(dp[1][i - 1] + arr[i], dp[0][i - 1] /*delete arr[i]*/));
}
return Arrays.stream(dp[1]).max().getAsInt();
}
}
// code provided by PROGIEZ
1186. Maximum Subarray Sum with One Deletion LeetCode Solution in Python
class Solution:
# Very similar to 53. Maximum Subarray
def maximumSum(self, arr: list[int]) -> int:
# dp[0][i] := the maximum sum subarray ending in i (no deletion)
# dp[1][i] := the maximum sum subarray ending in i (at most 1 deletion)
dp = [[0] * len(arr) for _ in range(2)]
dp[0][0] = arr[0]
dp[1][0] = arr[0]
for i in range(1, len(arr)):
dp[0][i] = max(arr[i], dp[0][i - 1] + arr[i])
dp[1][i] = max(arr[i], dp[1][i - 1] + arr[i], dp[0][i - 1])
return max(dp[1])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.