Skip to main content
LeetCode 1246, Hard. Topics: Array, Dynamic Programming. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 32 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026