All problems
1155EasyArrayHash TableGreedy

Keeping the Best Run of Different Chips

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3487Maximum Unique Subarray Sum After Deletion

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

Python
def max_sum(chips: list[int]) -> int:
Java
public int maxSum(int[] chips)
September 7
Apply