Leetcode 546. Remove Boxes
题目链接:
https://leetcode.com/problems/remove-boxes/description/
问题描述
若干个有序排列的box和它们的颜色,每次可以移除若干个连续的颜色相同的box,且得分为移除个数的平方,求最大的得分。
n不超过100.
输入
输入n个箱子的颜色。
输出
输出最大的得分。
样例输入
1, 3, 2, 2, 2, 3, 4, 3, 1
样例输出
23
题解
令dp[l][r][k]表示前面有k个和box[l]同颜色的要和box[l]一起消除,现在考虑l是不是最后一个消的,这样的话可以得到转移方程:dp[l][r][k]=max((k+1)^2+dp[l+1][r][0],dp[l+1][m-1][0]+dp[m][r][k+1])(l+1<=m<=r);
这道题在状态的表示上比较有技巧,主要是因为涉及到了子问题的self-contain的问题,具体的可以看推荐题解。
代码
int dp[101][101][101];
class Solution {
public:
int dfs(vector<int>& nums,int l,int r,int k){
if(l>r) return 0;
if(l==r) return (k+1)*(k+1);
if(dp[l][r][k]>=0) return dp[l][r][k];
int ll=l,rr=r,kk=k;
for(;l+1<=r&&nums[l]==nums[l+1];l++,k++);
dp[l][r][k]=dfs(nums,l+1,r,0)+(k+1)*(k+1);
for(int i=l+1;i<=r;i++){
if(nums[i]==nums[l]){
dp[l][r][k]=max(dp[l][r][k],dfs(nums,l+1,i-1,0)+dfs(nums,i,r,k+1));
}
}
return dp[ll][rr][kk]=dp[l][r][k];
}
int removeBoxes(vector<int>& nums) {
int n=nums.size();
for(int i=0;i<n;i++) for(int j=i;j<n;j++) for(int k=0;k<n;k++) dp[i][j][k]=-1;
return dfs(nums,0,n-1,0);
}
};
Leetcode 546. Remove Boxes的更多相关文章
- 第十周 Leetcode 546. Remove Boxes (HARD) 记忆化搜索
Leetcode546 给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分. dp方程如下: memo[l][r][k] = max(memo[l][r][k], dfs ...
- 546. Remove Boxes
Given several boxes with different colors represented by different positive numbers. You may experie ...
- 546 Remove Boxes 移除盒子
给定一些不同颜色的盒子,以不同的正整数表示.消去连续相同颜色的盒子,直到全部消除完毕为止.每一次消去可以得到k * k分(k为消去盒子的个数, k >= 1).计算可以得到的最大得分.注意:盒 ...
- [LeetCode] Remove Boxes 移除盒子
Given several boxes with different colors represented by different positive numbers. You may experie ...
- [Swift]LeetCode546. 移除盒子 | Remove Boxes
Given several boxes with different colors represented by different positive numbers. You may experie ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
随机推荐
- python pip常用命令
pip安装命令: pip install packagename pip显示模块版本号: pip show packagename pip卸载模块: pip uninstall packagename ...
- SDE ST_Geometry SQL st_intersects查询很慢的解决方法
环境:服务端 SDE 10.0 oracle 11.2,客户端 PLSQL 11,oracle 11.2 为了调试方便,以下测试都是把sql提取出来在PLSQL上做 需求是已知一个多边形的点坐标,要在 ...
- C#从入门到精通视频教程(2009年最新)- 视频列表
http://www.jz97.net/index.php/playlist/view/182/page/2 C#从入门到精通视频教程(2009年最新)- 视频列表
- 【SDOI2009】Bill的挑战
Description Sheng bill不仅有惊人的心算能力,还可以轻松地完成各种统计.在昨天的比赛中,你凭借优秀的程序与他打成了平局,这导致Sheng bill极度的不满.于是他再次挑战你.这次 ...
- spring.factories
在Spring Boot中有一种非常解耦的扩展机制:Spring Factories.这种扩展机制实际上是仿照Java中的SPI扩展机制来实现的. Java SPI机制SPI的全名为Service P ...
- 433 模块 ARDUINO测试
实验硬件 发射端 Arduino + 433超外差发射机 高,低电平和悬空三种模式切换 由简单的官方库修改 /* This is a minimal sketch without using ...
- DamonOehlman/detect-browser
https://github.com/DamonOehlman/detect-browser detect-browser This is a package that attempts to det ...
- ECS API
一.API调用方式 ➢对ECS API接口调用是通过向ECS API的服务端地址发送HTTP GET请求,并按照接口说明在请求 中加入相应请求参数来完成的;根据请求的处理情况,系统会返回处理结果. ➢ ...
- oracle 查询非自增长分区的最大分区
select a.table_owner, a.table_name, a.max_partition from (select table_owner, table_name, max(parti ...
- js删除数组元素、清空数组的简单方法
一.清空数组 ? 1 2 3 var ary = [1,2,3,4]; ary.splice(0,ary.length);//清空数组 console.log(ary); // 输出 [],空数组,即 ...