Maximize Subarray Sum After Removing All Occurrences of One Element Problem
Description
LeetCode Problem 3410.
You are given an integer array nums.
You can do the following operation on the array at most once:
- Choose any integer
xsuch thatnumsremains non-empty on removing all occurrences ofx. - Remove all occurrences of
xfrom the array.
Return the maximum subarray sum across all possible resulting arrays.
Example 1:
Input: nums = [-3,2,-2,-1,3,-2,3]
Output: 7
Explanation:
We can have the following arrays after at most one operation:
- The original array is nums = [-3, 2, -2, -1, 3, -2, 3]. The maximum subarray sum is 3 + (-2) + 3 = 4.
- Deleting all occurences of x = -3 results in nums = [2, -2, -1, 3, -2, 3]. The maximum subarray sum is 3 + (-2) + 3 = 4.
- Deleting all occurences of x = -2 results in nums = [-3, 2, -1, 3, 3]. The maximum subarray sum is 2 + (-1) + 3 + 3 = 7.
- Deleting all occurences of x = -1 results in nums = [-3, 2, -2, 3, -2, 3]. The maximum subarray sum is 3 + (-2) + 3 = 4.
- Deleting all occurences of x = 3 results in nums = [-3, 2, -2, -1, -2]. The maximum subarray sum is 2.
The output is max(4, 4, 7, 4, 2) = 7.
Example 2:
Input: nums = [1,2,3,4]
Output: 10
Explanation:
It is optimal to not perform any operations.
Constraints:
1 <= nums.length <= 10^5-10^6 <= nums[i] <= 10^6
Solution
Approach: Segment Tree
Removing a non-negative value never helps, so only the distinct negative values need to be tried (plus removing nothing).
Build a segment tree over nums. Each node stores the sum of its range and the best non-empty prefix, suffix and subarray sums, and two children merge in constant time (the best subarray is the best of the left child, the right child, or the left suffix joined with the right prefix). An "absent" element is a leaf that holds sum 0 and a very negative value for the other three fields, so it behaves as if it were deleted.
Group the indices by value. For each negative value x, turn all of its leaves into absent leaves, read the best subarray sum at the root (skip it if everything was removed), and put the leaves back. Each index is updated twice in total.
Complexity: O(n log n) time, O(n) space.
class Solution:
def maxSubarraySum(self, nums: List[int]) -> int:
NEG = -10**15
n = len(nums)
size = 1
while size < n:
size *= 2
sm = [0] * (2 * size)
pre = [NEG] * (2 * size)
suf = [NEG] * (2 * size)
best = [NEG] * (2 * size)
def pull(i):
while i > 1:
i >>= 1
l, r = 2 * i, 2 * i + 1
sm[i] = sm[l] + sm[r]
pre[i] = max(pre[l], sm[l] + pre[r])
suf[i] = max(suf[r], sm[r] + suf[l])
best[i] = max(best[l], best[r], suf[l] + pre[r])
for i, x in enumerate(nums):
j = size + i
sm[j] = pre[j] = suf[j] = best[j] = x
for i in range(size - 1, 0, -1):
l, r = 2 * i, 2 * i + 1
sm[i] = sm[l] + sm[r]
pre[i] = max(pre[l], sm[l] + pre[r])
suf[i] = max(suf[r], sm[r] + suf[l])
best[i] = max(best[l], best[r], suf[l] + pre[r])
pos = {}
for i, x in enumerate(nums):
if x < 0:
pos.setdefault(x, []).append(i)
ans = best[1]
for x, ids in pos.items():
for i in ids:
j = size + i
sm[j] = 0
pre[j] = suf[j] = best[j] = NEG
pull(j)
if best[1] > NEG // 10:
ans = max(ans, best[1])
for i in ids:
j = size + i
sm[j] = pre[j] = suf[j] = best[j] = x
pull(j)
return ans