Show problems as
Classic problem #1695MediumArrayHash TableSliding Window

Maximum Erasure Value

Real-world version: Richest Run Of Distinct Bins
Tracked in this browser only
Read Maximum Erasure Value on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A picking aisle is a row of bins. bins[i] is the part number stored in bin i, and clearing a bin earns that many points. The same part number may be stored in more than one bin.

A picker clears one contiguous stretch of bins, and the audit only accepts the run when no part number appears twice inside it. Return the largest number of points such a run can earn.

Examples

Example 1

Input
bins = [1, 5, 4, 5]
Output
10

Clearing bins 0 through 2 collects part numbers 1, 5 and 4, which are all different, for 10 points. Adding bin 3 would repeat part number 5, so that run is not accepted.

Example 2

Input
bins = [4, 2, 7, 9]
Output
22

No part number appears twice anywhere, so the whole aisle is one accepted run worth 4 + 2 + 7 + 9 = 22 points.

Example 3

Input
bins = [2, 1, 2, 1, 2, 1, 2]
Output
3

Any accepted run holds at most one bin of part number 2 and one of part number 1, so a run such as bins 0 and 1 earns 3 points.

Constraints

  • 1 <= bins.length <= 10^5
  • 1 <= bins[i] <= 10^4

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def maximum_unique_subarray(bins: list[int]) -> int:
Java
public int maximumUniqueSubarray(int[] bins)