> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Remove Boxes Python Solution with Tests

> Tested Python solution for LeetCode 546 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 546, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Memoization](/catalog/topics/memoization). [View on LeetCode](https://leetcode.com/problems/remove-boxes/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 546   # by problem number
lcpy gen -s remove_boxes   # by problem name
```

## 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*.

### Examples

```
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)
```

```
Input: boxes = [1,1,1]
Output: 9
```

```
Input: boxes = [1]
Output: 1
```

### Constraints

* `1 <= boxes.length <= 100`
* `1 <= boxes[i] <= 100`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_boxes/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_boxes/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from functools import cache


class Solution:
    # Time: O(n^4) worst case, Space: O(n^3)
    def remove_boxes(self, boxes: list[int]) -> int:
        @cache
        def dp(left: int, right: int, k: int) -> int:
            if left > right:
                return 0
            # Grow the leftmost run so boxes[left..end] share one color.
            end = left
            while end + 1 <= right and boxes[end + 1] == boxes[end]:
                end += 1
            attached = k + (end - left + 1)
            best = attached * attached + dp(end + 1, right, 0)
            for mid in range(end + 1, right + 1):
                if boxes[mid] == boxes[left] and boxes[mid - 1] != boxes[mid]:
                    best = max(
                        best,
                        dp(end + 1, mid - 1, 0) + dp(mid, right, attached),
                    )
            return best

        return dp(0, len(boxes) - 1, 0)
```

## Complexity

| Time | Space |
| - | - |
| O(n^4) worst case, Space: O(n^3) | - |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.