Remove Boxes - Problem
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 (i.e., composed of k boxes, k >= 1), remove them and get k * k points.
Return the maximum points you can get.
Input & Output
Example 1 — Basic Case
$
Input:
boxes = [1,3,2,2,2,3,4,3,1]
›
Output:
23
💡 Note:
Remove three 2s (3×3=9 points), then remove two 3s (2×2=4 points), then remove remaining boxes one by one. Total: 9+4+1+1+1+1+1+1+1+1+1+1 = 23 points.
Example 2 — All Same Color
$
Input:
boxes = [1,1,1]
›
Output:
9
💡 Note:
Remove all three boxes at once: 3×3 = 9 points.
Example 3 — Single Box
$
Input:
boxes = [1]
›
Output:
1
💡 Note:
Only one box to remove: 1×1 = 1 point.
Constraints
- 1 ≤ boxes.length ≤ 100
- 1 ≤ boxes[i] ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code