Maximum Subarray Sum with One Deletion - Complete Solution Guide
Maximum Subarray Sum with One Deletion is LeetCode problem 1186, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks us to find the maximum sum of a subarray within a given array of integers, with the option to delete at most one element from the subarray. The chosen subarray must be non-empty after the (optional) deletion. We need to consider all possible subarrays and, for each subarray, consider both the case where we don't delete any element and the case where we delete one element to maximize the sum.
Solution Approach
The solution uses dynamic programming to maintain two states: `no_del` which stores the maximum sum of a subarray ending at the current index without any deletions, and `one_del` which stores the maximum sum of a subarray ending at the current index with at most one deletion. For each index, we update these states and keep track of the overall maximum sum encountered so far. The approach is based on Kadane's Algorithm, but extended to handle the single deletion.
Step-by-Step Algorithm
- Step 1: Initialize `no_del` and `max_so_far` to the first element of the array. Initialize `one_del` to negative infinity, since at the start there are no elements to delete from before the second element.
- Step 2: Iterate through the array from the second element (index 1) to the end.
- Step 3: For each index `i`, update `one_del`. We have two choices: (a) Extend the previous subarray with one deletion by adding the current element `arr[i]`, or (b) Start a new subarray by deleting the current element from a subarray ending at the previous index with no deletion. So, `one_del = max(one_del + arr[i], no_del)`.
- Step 4: Update `no_del` using Kadane's algorithm: either extend the previous subarray with no deletion by adding the current element, or start a new subarray from the current element. `no_del = max(no_del + arr[i], arr[i])`.
- Step 5: Update `max_so_far` to be the maximum of the current `max_so_far`, `no_del`, and `one_del`. This ensures we keep track of the overall maximum sum we have encountered.
- Step 6: Return `max_so_far`.
Key Insights
- Insight 1: Dynamic Programming is key. We can keep track of maximum subarray sums ending at each index, with and without a deletion.
- Insight 2: We need two states for each index: the maximum subarray sum ending at that index with no deletion, and the maximum subarray sum ending at that index with one deletion.
- Insight 3: The 'one deletion' state depends on the previous 'no deletion' state, so it's crucial to update them in the correct order.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming.
Companies
Asked at: Two Sigma.