All problems
0635MediumArrayHash TableSliding Window

Richest Run Of Distinct Bins

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1695Maximum Erasure Value

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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