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

# Palindrome Removal Python Solution with Tests

> Tested Python solution for LeetCode 1246 with 32 pytest cases. Generate a practice environment with lcpy.

LeetCode 1246, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/palindrome-removal/description/).

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

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

## Problem

You are given an integer array `arr`.

In one move, you can select a **palindromic** subarray `arr[i], arr[i + 1], ..., arr[j]` where `i <= j`, and remove that subarray from the given array. Note that after removing a subarray, the elements on the left and on the right of that subarray move to fill the gap left by the removal.

Return the minimum number of moves needed to remove all numbers from the array.

### Examples

```
Input: arr = [1,2]
Output: 2
```

```
Input: arr = [1,3,4,1,5]
Output: 3
```

**Explanation:** Remove `[4]` then remove `[1,3,1]` then remove `[5]`.

### Constraints

* `1 <= arr.length <= 100`
* `1 <= arr[i] <= 20`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^3)
    # Space: O(n^2)
    def minimum_moves(self, arr: list[int]) -> int:
        n = len(arr)
        # dp[i][j] = minimum moves to clear arr[i..j]
        dp = [[0] * n for _ in range(n)]
        for i in range(n):
            dp[i][i] = 1
        for i in range(n - 2, -1, -1):
            for j in range(i + 1, n):
                if i + 1 == j:
                    dp[i][j] = 1 if arr[i] == arr[j] else 2
                    continue
                best = n
                if arr[i] == arr[j]:
                    best = dp[i + 1][j - 1]
                for k in range(i, j):
                    best = min(best, dp[i][k] + dp[k + 1][j])
                dp[i][j] = best
        return dp[0][n - 1]
```

## Complexity

| Time | Space |
| - | - |
| O(n^3) | O(n^2) |

## Tags


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