Remove Boxes - Problem
Remove Boxes - Maximum Points Game

Imagine you have a row of colorful boxes, each represented by a positive number indicating its color. Your goal is to remove all boxes and maximize your score in the process!

Game Rules:
• You can remove any continuous sequence of boxes with the same color
• If you remove k consecutive boxes of the same color, you earn k × k points
• Continue until no boxes remain

Strategy Example: For boxes [1,3,2,2,2,3,4,3,1], you could remove the middle three 2's first (earning 3×3=9 points), which brings the 3's together for a potentially higher score later!

Return the maximum total points you can achieve with optimal play.

Input & Output

example_1.py — Basic Case
$ Input: [1,3,2,2,2,3,4,3,1]
Output: 23
💡 Note: Remove middle three 2's first (3×3=9 points), then remove the two 3's together (2×2=4 points), then remove remaining boxes individually (1+1+1+1=4 points). Total: 9+4+4+1+1+1+1+1 = 23 points.
example_2.py — Simple Consecutive
$ Input: [1,1,1]
Output: 9
💡 Note: Remove all three 1's at once to get 3×3 = 9 points. This is better than removing them separately (1+1+1 = 3 points).
example_3.py — Single Box
$ Input: [1]
Output: 1
💡 Note: Only one box, so we remove it and get 1×1 = 1 point.

Constraints

  • 1 ≤ boxes.length ≤ 100
  • 1 ≤ boxes[i] ≤ 100
  • Each box color is represented by a positive integer
  • The array can contain duplicate colors in non-consecutive positions

Visualization

Tap to expand
Strategic Box Removal - Key InsightSometimes removing middle boxes creates better combinations later!Initial State: [1,3,2,2,2,3,4,3,1]132223431❌ Greedy Approach: Remove consecutive groups immediatelyRemove three 2's: 3² = 9 points, then singles: 1+1+1+1+1+1 = 6 points → Total: 15✅ Optimal Strategy: Remove middle 2's to combine 3'sStep 1: Remove three 2's → 9 points, creates [1,3,3,4,3,1]Step 2: Remove 4 → 1 point, creates [1,3,3,3,1]Step 3: Remove three 3's → 9 points, remove two 1's → 2 pointsTotal: 9 + 1 + 9 + 1 + 1 = 21 points!🧠 Key Insight: Use 3D DP dp[i][j][k] to track potential future combinationsk represents extra boxes of same color that could be combined later
Understanding the Visualization
1
Analyze the Array
Identify potential same-color groups that could be combined if middle elements are removed first
2
Strategic Removal
Instead of greedy removal, consider removing middle sections to bring same colors together
3
Dynamic Programming
Use 3D DP to track all possible states and find optimal removal sequence
4
Maximize Score
Choose the sequence that maximizes total points with k² scoring system
Key Takeaway
🎯 Key Insight: The optimal solution uses 3D dynamic programming to consider both immediate removal and strategic delayed removal. The third dimension k tracks potential future combinations, allowing the algorithm to make optimal decisions about when to remove boxes versus when to wait for better combos.
Asked in
Google 42 Amazon 28 Meta 15 Microsoft 12
52.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen