You have an array of positive integers and need to find the most valuable subarray you can "erase" - but there's a catch! The subarray must contain only unique elements (no duplicates).
Your score is the sum of all elements in the erased subarray. Your goal is to find the maximum possible score by choosing the optimal contiguous subarray.
Example: Given [4,2,4,5,6], you could erase [4,2] (score = 6), [2,4,5,6] (score = 17), or [4,5,6] (score = 15). The maximum score is 17.
Think of it as a sliding window problem where you want to find the longest valid window with the highest sum!
Input & Output
Visualization
Time & Space Complexity
Each element is visited at most twice (once by right pointer, once by left pointer)
Hash map to store element positions, worst case stores all elements
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 104
- All elements are positive integers