#546

Remove Boxes

candidate master · 1440 · lc hard +32 · verified · 49.4% accepted · 2,462 likes · top 36%

play →

Description

You have an array boxes where each integer represents a box color. In each round you remove any group of adjacent boxes that all share the same color; removing k boxes earns k * k points. Continue removing until no boxes remain. Return the maximum total score you can achieve.

Example 1:

Input: boxes = [1,3,2,2,2,3,4,3,1]
Output: 23
Explanation:
[1, 3, 2, 2, 2, 3, 4, 3, 1]
----> [1, 3, 3, 4, 3, 1] (3*3=9 points)
----> [1, 3, 3, 3, 1] (1*1=1 points)
----> [1, 1] (3*3=9 points)
----> [] (2*2=4 points)

Example 2:

Input: boxes = [1,1,1]
Output: 9

Example 3:

Input: boxes = [1]
Output: 1

Code

1
2
3