Array Greedy Hash Table 3487. Maximum Unique Subarray Sum After Deletion ¶ Time: $O(n)$ Space: $O(n)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11class Solution { public: int maxSum(vector<int>& nums) { const int mx = ranges::max(nums); if (mx <= 0) return mx; unordered_set<int> numsSet(nums.begin(), nums.end()); return accumulate(numsSet.begin(), numsSet.end(), 0, [](int acc, int num) { return acc + max(0, num); }); } }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public int maxSum(int[] nums) { final int mx = Arrays.stream(nums).max().getAsInt(); if (mx <= 0) return mx; return Arrays.stream(nums) .boxed() .collect(Collectors.toSet()) .stream() .filter(num -> num > 0) .mapToInt(Integer::intValue) .sum(); } } 1 2 3 4 5 6class Solution: def maxSum(self, nums: list[int]) -> int: mx = max(nums) if mx <= 0: return mx return sum(max(0, num) for num in set(nums)) Was this page helpful? Thanks for your feedback! Thanks for your feedback! Help us improve this page by using our feedback form.