代码拉取完成,页面将自动刷新
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:
Input: arr = [1,2]
Output: 2
Example 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
class Solution {
public:
int dp[100][100];
int minimumMoves(vector<int>& A) {
memset(dp,-1,sizeof(dp));
return dfs(A,0,A.size()-1);
}
int dfs(vector<int>& A,int l,int r){
if(l>=r)return 1;
if(dp[l][r]!=-1)return dp[l][r];
int res=INT_MAX;
if(A[l]==A[r]){
res=dfs(A,l+1,r-1);
}
for(int i=l;i<r;i++){
res=min(res,dfs(A,l,i)+dfs(A,i+1,r));
}
dp[l][r]=res;
return res;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。