leetcode-1246-Palindrome Removal
问题
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.
Example 1:
1 | Input: arr = [1,2] |
Example 2:
1 | Input: arr = [1,3,4,1,5] |
Constraints:
1 <= arr.length <= 100
1 <= arr[i] <= 20
分析
每个字段 $arr[i] … arr[j]$ 要么 $arr[i]$ 和 $arr[j]$ 一起“带走”,要么分两次以上带走。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/04/20/leetcode-1246/
License: 知识共享署名-非商业性使用 4.0 国际许可协议