Maximum Erasure Value

You are given an array of positive integers `nums`. You may erase exactly one subarray whose elements are all **unique**. The score of the erasure is the sum of the erased elements. Return the **maximum score** you can get by erasing exactly one such subarray. A subarray is a contiguous, non-empty part of the array.

30:00

Maximum Erasure Value
medium
Free written solution
Topics
Companies

You are given an array of positive integers nums. You may erase exactly one subarray whose elements are all unique. The score of the erasure is the sum of the erased elements.

Return the maximum score you can get by erasing exactly one such subarray. A subarray is a contiguous, non-empty part of the array.

Example 1:
Input: {"nums":[4,2,4,5,6]}
Output: 17
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • 1≤nums[i]≤1041 \leq \text{nums}[i] \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[4,2,4,5,6]}

The window to erase starts empty: no values seen, cur = 0, best = 0. It will grow to the right one value at a time.

nums: teal = window to erase, green line = best window

4

L

2

1

4

2

5

3

6

4

seen = values in the window (0)

{ }

cur

0

best

0

Variables
VariableValue
left0
right-1
cur0
best0
DepthFunction Call
Stack empty
0/13