Skip to problem
←

Remove Boxes

Hard 0 attempts

Given a row of colored boxes represented by integers in boxes, remove every box while maximizing your total score.

In one move, choose a nonempty consecutive group of k boxes that all have the same color. Remove them and earn k² points. The remaining boxes close the gap, so equal colors that were separated may become adjacent. You may choose any such group; it need not be a maximal run.

Return the highest total score, not the removal order. The integer labels identify colors and do not affect points.

For [1, 2, 1], remove the middle 2 for 1 point, then the two adjacent 1s for 4, totaling 5. Removing the leftmost box first would lead to only 3 points. Delaying a removal can therefore be better than removing an available group immediately.

Function contract

Implement removeBoxes(boxes).

Output: Return the maximum total score after removing every box.

Example 1

Input
boxes = [1,3,2,2,2,3,4,3,1]
Output
23

Example 2

Input
boxes = [1,1,1]
Output
9
Constraints
  • 1 <= boxes.length <= 100
  • 1 <= boxes[i] <= 100

3D Interval DP

Let solve(l, r, k) be the best score for boxes[l..r] with k extra boxes of the same color as boxes[r] attached to its right. The extra count records boxes saved for a future merge; it does not change the input.

There are two choices for the rightmost color:

  • Remove it with the carried boxes now, scoring (k + 1)², and solve the remaining interval independently.
  • Choose an earlier matching color at index i. First clear the intervening interval [i + 1, r - 1], then carry the rightmost box and its k companions into solve(l, i, k + 1).

Try every earlier matching position and keep the best score. Keeping equal colors together is sufficient for an optimal solution: combining groups of sizes a and b earns (a + b)² >= a² + b², so splitting an already adjacent same-color group cannot improve the score.

Memoize the interval and carried count. An empty interval scores zero; every nonempty state has a positive score, making zero a safe marker for an uncomputed state.

javascript
function removeBoxes(boxes) {
  const n = boxes.length;
  const dp = Array.from({ length: n }, () =>
    Array.from({ length: n }, () => Array(n).fill(0)),
  );
  function solve(l, r, k) {
    if (l > r) return 0;
    if (dp[l][r][k]) return dp[l][r][k];
    let res = solve(l, r - 1, 0) + (k + 1) * (k + 1);
    for (let i = l; i < r; i++)
      if (boxes[i] === boxes[r])
        res = Math.max(res, solve(l, i, k + 1) + solve(i + 1, r - 1, 0));
    return (dp[l][r][k] = res);
  }
  return solve(0, n - 1, 0);
}

Time: O(n⁴) | Space: O(n³)

Loading discussion…

Saved in this browser only. Private to you.

JavaScript