Imagine you're cleaning up a messy array by removing palindromic sections piece by piece! ๐งน
You are given an integer array arr. In one move, you can select any palindromic subarray arr[i], arr[i + 1], ..., arr[j] where i <= j, and completely remove that subarray from the array. After removal, the remaining elements automatically shift together to fill the gap.
A palindromic subarray reads the same forwards and backwards. For example: [1], [2,2], [3,1,3], or [4,5,4].
Goal: Return the minimum number of moves needed to remove all elements from the array.
Example: For array [1,3,4,1,5], you could remove [4] first (1 move), then [3] (1 move), then [1,5,1] is not palindromic, so remove each [1] and [5] separately (2 more moves) = 4 total moves. But there might be a better strategy!
Input & Output
Constraints
- 1 โค arr.length โค 100
- 1 โค arr[i] โค 20
- Each element is a positive integer
- Array is not empty