Hard

Quiz

#546 Remove Boxes

APPROACH

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
1 of 4
1:00

What is the optimal approach for this problem?