Remove Boxes

You are given several boxes with different colors represented by different positive numbers.

You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color, composed of k boxes where k >= 1, remove them, and get k * k points.

Return the maximum points you can get.

Example 1
Inputboxes = [1,3,2,2,2,3,4,3,1]
Output23
Removing the three 2s, then the 4, then the three 3s, then the two 1s gives 9 + 1 + 9 + 4 = 23 points.
Example 2
Inputboxes = [1,1,1]
Output9
All three boxes have the same color, so removing them together gives 3 * 3 = 9 points.

Constraints

  • 1 <= boxes.length <= 100
  • 1 <= boxes[i] <= 100

Asked at 10 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate