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 ...
随机推荐
- hadoop系列 第二坑: hive hbase关联表问题
关键词: hive创建表卡住了 创建hive和hbase关联表卡住了 其实针对这一问题在info级别的日志下是看出哪里有问题的(为什么只能在debug下才能看见呢,不太理解开发者的想法). 以调试模式 ...
- ThinkPHP5.0 实现 app微信支付功能
相对于之前随笔写的<ThinkPHP5.0实现app支付宝支付功能>来说,php对接app微信支付功能就相对简单的多了,最近有加我的朋友问到app微信支付,所以我把app微信支付的demo ...
- 六大主流开源SQL引擎
导读 本文涵盖了6个开源领导者:Hive.Impala.Spark SQL.Drill.HAWQ 以及Presto,还加上Calcite.Kylin.Phoenix.Tajo 和Trafodion.以 ...
- vue分页全选和单选操作
<!DOCTYPE html> <html> <head> <title>演示Vue</title> <style> ul,li ...
- <转>jmeter(十四)HTTP请求之content-type
本博客转载自:http://www.cnblogs.com/dinghanhua/p/5646435.html 个人感觉不错,对jmeter最常用的取样器http请求需要用到的信息头管理器做了很好的解 ...
- Kubernetes 1.10.4 镜像 版本
1. gcr.io/google-containers/hyperkube:1.10.4 gcr.io/google_containers/pause-amd64:3.0 gcr.io/google_ ...
- 车轮升级PHP7踩过的一些坑
社区php7升级记录 社区服务器已经全部完成升级,这里记录一下社区升级php7所遇到的问题,可以分为四个类型 扩展支持的变化,导致需要修改配置甚至调整替换操作的类库 php7语法检查比之前变得严格,部 ...
- 初识Identity(二)
本文参考了[ASP.NET Identity系列教程(一)]ASP.NET Identity入门 一.Identity使用前准备项目 本文创建了一个名称为Users的项目.在创建过程中选择了“Empt ...
- ASP.NET MVC学习笔记(二)笔记
接下来我们一起了解ASP.NET MVC的最重要的核心技术,了解ASP.NET MVC的开发框架,生命周期,技术细节. 一.Routing与ASP.NET MVC生命周期 1.Routing——网址路 ...
- Struts学习总结-02 上传文件
Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的目录,以确保数据不丢失. ...