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.
Input: nums =[1,1,0,1,1]Output: 1Explanation:
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.
Input: nums =[1,2,-1,-2,1,0,-1]Output: 3Explanation:
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.
To maximize the sum of a subarray with unique elements, we need to find continuous windows without repetitions, and among all these, choose the one with the largest possible sum.
classSolution:
defmaxSum(self, nums: list[int]) -> int:
seen: set[int] = set()
sum_ =0 max_ = float('-inf')
for num in nums:
if num >0and num notin seen:
seen.add(num)
sum_ += num
max_ = max(max_, num)
return sum_ if sum_ >0else max_
classSolution {
intmaxSum(int[] nums) {
Set<Integer> seen =new HashSet<>();
int sum = 0;
int max = Integer.MIN_VALUE;
for (int num : nums) {
if (num > 0 && seen.add(num)) {
sum += num;
}
max = Math.max(max, num);
}
return (sum > 0) ? sum : max;
}
}
classSolution {
funmaxSum(nums: IntArray): Int {
val seen = mutableSetOf<Int>()
var sum = 0var maxVal = Int.MIN_VALUE
for (num in nums) {
if (num > 0&& seen.add(num)) {
sum += num
}
maxVal = maxOf(maxVal, num)
}
returnif (sum > 0) sum else maxVal
}
}
At first glance, the problem seems suited for a sliding window approach, as it asks for a maximum subarray sum.
However, the key insight is that since we can delete any elements, we should simply keep all unique positive numbers. This guarantees the largest possible sum, as negative numbers and duplicates only reduce the total. For example, given [1,2,-1,-2,1,0,-1], we can remove all negatives, then keep only one of each positive, resulting in [1,2,0] and a sum of 3.
classSolution {
publicintmaxSum(int[] nums) {
int n = nums.length;
if (n == 1) {
return nums[0];
}
int max_one = Arrays.stream(nums).max().getAsInt();
if (max_one < 0) {
return max_one;
}
Set<Integer> uniquePositives =new HashSet<>();
int maxSum = 0;
for (int num : nums) {
if (num > 0 &&!uniquePositives.contains(num)) {
uniquePositives.add(num);
maxSum += num;
}
}
return maxSum;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funmaxSum(nums: IntArray): Int {
if (nums.size ==1) return nums[0]
val maxOne = nums.maxOrNull() ?:Int.MIN_VALUE
if (maxOne < 0) return maxOne
val uniquePositives = mutableSetOf<Int>()
var maxSum = 0for (num in nums) {
if (num > 0&& uniquePositives.add(num)) {
maxSum += num
}
}
return maxSum
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defmaxSum(self, nums: list[int]) -> int:
if len(nums) ==1:
return nums[0]
max_one = max(nums)
if max_one <0:
return max_one
unique_positives = set()
max_sum =0for num in nums:
if num >0and num notin unique_positives:
unique_positives.add(num)
max_sum += num
return max_sum