↓Skip to main content
  1. LeetCode/

LeetCode 1186: Maximum Subarray Sum with One Deletion

·2 mins· ·
LeetCode Medium Array Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-10-22

  • Submissions: Wrong Answer → Accepted

Approach
#

Kadane (Kadane’s Algorithm) with one extra state. At each index i, keep two values, both for subarrays ending at i:

  • no_delete: the best sum with no deletion, max(no_delete + num, num), the usual Kadane.
  • delete_one: the best sum with exactly one deletion. Two sources:
    • Delete the current num: the sum of the best undeleted subarray ending just before, i.e. the previous no_delete.
    • A deletion already happened earlier: delete_one + num.

Update both from the previous step’s values at the same time. The subarray must keep at least one element, so delete_one starts at -inf and the first element cannot be deleted into an empty subarray.

The answer is the maximum of both values over all indices, updated at every step.

My Attempts
#

  1. Returned max(no_delete, delete_one) only at the end. Those states describe subarrays ending at the last element, but the best one may end earlier: [1,-1,-3] returned 0, the answer is 1.
  2. Updating the answer at every step passed.

Solution
#

class Solution:
    def maximumSum(self, arr: List[int]) -> int:
        no_delete = -inf
        delete_one = -inf
        best = -inf
        for num in arr:
            no_delete, delete_one = max(no_delete + num, num), max(no_delete, delete_one + num)
            best = max(best, no_delete, delete_one)
        return best

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • The DP state means “ends here”, so take the maximum at every step, not only at the last one.
  • Update both states simultaneously (tuple assignment); delete_one needs the old no_delete.
  • If every value is negative the answer is the largest element, since the array cannot be deleted to empty; start from -inf, not 0.

Interview Explanation
#

It is Kadane with one more state: the best sum ending at i with no deletion, and with one deletion. One deletion comes either from deleting the current element after the previous undeleted best, or from an earlier deletion plus the current element. Since the states mean “ends here”, I update the answer at every step. Time O(n), space O(1).

Related

LeetCode 3638: Maximum Balanced Shipments
·2 mins
LeetCode Medium Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 3638 note: greedily cut a segment as soon as a value below its maximum appears; until then the segment is non-decreasing, so its maximum is the previous value
LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 396: Rotate Function
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 396 note: F(k) and F(k - 1) differ by one formula: F(k) = F(k - 1) + sum - n × the element moved to the front
LeetCode 435: Non-overlapping Intervals
·3 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 473: Matchsticks to Square
·2 mins
LeetCode Medium Array Dynamic-Programming Backtracking Bit-Manipulation
LeetCode 473 note: the total must be divisible by 4; place sticks from longest to shortest into one of four sides, skipping a side that would overflow or that equals a side already tried
LeetCode 4040: Minimum Operations to Form Subset Sum I
·2 mins
LeetCode Medium Dynamic-Programming Knapsack Array
LeetCode 4040 note: with multiplications before divisions, the best is always multiply-only or divide-only; list each number’s reachable values and costs, then run a grouped 0/1 knapsack