3487. Maximum Unique Subarray Sum After Deletion LeetCode Solution
In this guide, you will get 3487. Maximum Unique Subarray Sum After Deletion LeetCode Solution with the best time and space complexity. The solution to Maximum Unique Subarray Sum After Deletion problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Unique Subarray Sum After Deletion solution in C++
- Maximum Unique Subarray Sum After Deletion solution in Java
- Maximum Unique Subarray Sum After Deletion solution in Python
- Additional Resources
Problem Statement of Maximum Unique Subarray Sum After Deletion
You are given an integer array nums.
You are allowed to delete any number of elements from nums without making it empty. After performing the deletions, select a subarray of nums such that:
All elements in the subarray are unique.
The sum of the elements in the subarray is maximized.
Return the maximum sum of such a subarray.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 15
Explanation:
Select the entire array without deleting any element to obtain the maximum sum.
Example 2:
Input: nums = [1,1,0,1,1]
Output: 1
Explanation:
Delete the element nums[0] == 1, nums[1] == 1, nums[2] == 0, and nums[3] == 1. Select the entire array [1] to obtain the maximum sum.
Example 3:
Input: nums = [1,2,-1,-2,1,0,-1]
Output: 3
Explanation:
Delete the elements nums[2] == -1 and nums[3] == -2, and select the subarray [2, 1] from [1, 2, 1, 0, -1] to obtain the maximum sum.
Constraints:
1 <= nums.length <= 100
-100 <= nums[i] <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3487. Maximum Unique Subarray Sum After Deletion LeetCode Solution in C++
class 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); });
}
};
/* code provided by PROGIEZ */
3487. Maximum Unique Subarray Sum After Deletion LeetCode Solution in Java
class 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();
}
}
// code provided by PROGIEZ
3487. Maximum Unique Subarray Sum After Deletion LeetCode Solution in Python
class 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))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.