Show problems as
Classic problem #3487EasyArrayHash TableGreedy

Maximum Unique Subarray Sum After Deletion

Real-world version: Keeping the Best Run of Different Chips
Tracked in this browser only
Read Maximum Unique Subarray Sum After Deletion on LeetCode(opens in a new tab)

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

A tray holds chips whose values are chips. Values may repeat and may be below zero.

Discard as many chips as you like, so long as at least one is left. Then choose a run of neighbouring survivors in which no value turns up twice, and add its values up.

Return the largest total that can be reached.

Examples

Example 1

Input
chips = [1, 2, 3, 3, 2, 1]
Output
6

Discarding the repeats leaves 1, 2 and 3 side by side, and every value above zero counted once comes to 6.

Example 2

Input
chips = [-3, -1, -2]
Output
-1

Every chip is below zero and one has to be kept, so the least bad single chip is the best on offer.

Example 3

Input
chips = [-5, 4, -3, 4, 2]
Output
6

Discarding the two negatives and one of the two 4 chips leaves 4 and 2 next to each other for 6.

Constraints

  • 1 <= chips.length <= 100
  • -100 <= chips[i] <= 100

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 max_sum(chips: list[int]) -> int:
Java
public int maxSum(int[] chips)