Description
Given an array of positive integers nums, return the maximum sum of a subarray with unique elements (no duplicate values).
Examples
Input:
nums = [4,2,4,5,6]Output:
17Explanation:
[2,4,5,6] = 17.
Input:
nums = [1,2,1,3,4,3,2,5]Output:
10Explanation:
The maximum sum subarray with all unique elements is [1,3,4,3,2,5] — removing duplicates, the best contiguous unique subarray sums to 10.
Input:
nums = [1,1,1,1,1]Output:
1Explanation:
Since all elements are the same, any subarray with unique elements can contain at most one element. The maximum sum is 1.
Constraints
- •
1 ≤ nums.length ≤ 10⁵