↓Skip to main content
  1. LeetCode/

LeetCode 3487: Maximum Unique Subarray Sum After Deletion

·2 mins· ·
LeetCode Easy Greedy Hash-Table Array
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: easy First Attempt: 2025-03-16

  • Submissions: Wrong Answer ×2 → Accepted (2025-07-25)

Approach
#

Any elements may be deleted (just not all), so any set of elements with distinct values can be left behind as a subarray. The problem becomes: pick distinct values with the largest sum.

  • Take one of every non-negative value (0 does not change the sum).
  • If everything is negative, something must remain, so keep the largest.

This is the “sort then scan” form from Greedy, without even needing the sort.

My Attempts
#

VersionProblem
Attempt 1Collected only positives; with all negatives the set was empty and it returned 0. For [-3] the answer is -3
Attempt 2Special-cased length 1, but [-5, -1] still returned 0 instead of -1
ACWhen the set is empty, return the largest negative

Solution
#

class Solution:
    def maxSum(self, nums: List[int]) -> int:
        keep = set(x for x in nums if x >= 0)
        return sum(keep) if keep else max(nums)

Complexity
#

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

Mistakes to Avoid
#

  • The result cannot be empty: with all negatives, return the maximum, not 0.
  • Special-casing length 1 is not enough; the real check is whether any non-negative value exists.

Interview Explanation
#

Since deletion is free, any set of distinct values can become the subarray, so I add each non-negative value once. If there are none, one element must remain, so I return the largest negative. Time O(n).

Related

LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values
LeetCode 3074: Apple Redistribution into Boxes
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 3074 note: apples can be split freely, so take boxes from the largest capacity until the total fits every apple
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2335: Minimum Amount of Time to Fill Cups
·1 min
LeetCode Easy Array Greedy Sorting Heap-Priority-Queue
LeetCode 2335 note: the answer is max(the largest count, ceil(total / 2)); simulating means filling one cup from each of the two largest every second
LeetCode 1122: Relative Sort Array
·1 min
LeetCode Easy Array Hash-Table Sorting Counting-Sort
LeetCode 1122 note: count first, emit values in arr2’s order, then append the remaining values in ascending order
LeetCode 1128: Number of Equivalent Domino Pairs
·2 mins
LeetCode Easy Array Hash-Table Counting
LeetCode 1128 note: normalize each domino to (small, large) as a key and count; a key with c dominoes yields c(c - 1) / 2 pairs