题目链接:

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的更多相关文章

  1. 第十周 Leetcode 546. Remove Boxes (HARD) 记忆化搜索

    Leetcode546 给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分. dp方程如下: memo[l][r][k] = max(memo[l][r][k], dfs ...

  2. 546. Remove Boxes

    Given several boxes with different colors represented by different positive numbers. You may experie ...

  3. 546 Remove Boxes 移除盒子

    给定一些不同颜色的盒子,以不同的正整数表示.消去连续相同颜色的盒子,直到全部消除完毕为止.每一次消去可以得到k * k分(k为消去盒子的个数, k  >= 1).计算可以得到的最大得分.注意:盒 ...

  4. [LeetCode] Remove Boxes 移除盒子

    Given several boxes with different colors represented by different positive numbers. You may experie ...

  5. [Swift]LeetCode546. 移除盒子 | Remove Boxes

    Given several boxes with different colors represented by different positive numbers. You may experie ...

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  7. 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++> 给出排序好的 ...

  8. [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% ...

  9. [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 ...

随机推荐

  1. cobaltstrike3.8服务器搭建及使用

    参考链接: https://www.ezreal.net/archives/166.htmlhttp://blog.cobaltstrike.com/category/cobalt-strike-2/ ...

  2. 在pycharm中每次运行代码不使用console而使用run

    问题:在pycharm中点击run运行程序,发现没有打开run窗口,而是打开的Python console窗口. 解决方法:打开菜单栏run->edit configurations,把下图中的 ...

  3. Python进阶(二)

    高阶函数 1.把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式. 2.Python内建了map( )和reduce( ) 函数 map()函数接收两个参数,一个是函数 ...

  4. Python3编写网络爬虫11-数据存储方式四-关系型数据库存储

    关系型数据库存储 关系型数据库是基于关系模型的数据库,而关系模型是通过二维表保存的,所以它的存储方式就是行列组成的表.每一列是一个字段,每一行是一条记录.表可以看作某个实体的集合,而实体之间存在联系, ...

  5. JAVA中MAP转LIST

    @Test public void testMap2List() throws Exception{     Map<String, String> map = new HashMap&l ...

  6. Android Studio入门问题汇总

    1.如何设置 AS 中的字体大小 2.如何切换 AS 的皮肤颜色,默认为黑色,修改为白色,改为 default 3.首次安装 Android Studio并打开时,如果创建了一个新工程并将工程保存在另 ...

  7. activiti获取可回退的节点

    在处理流程回退时,需要获取某个节点当前可以回退到的节点,简单分析下: 1. 只支持回退到userTask. 2. 如果流程流转过某节点时生成了多个任务,从其中某一个任务回退到该节点后,不处理另外的任务 ...

  8. RestTemplate 服务名请求

    @loadBalance注解修饰的restTemplate才能实现服务名的调用,没有修饰的restTemplate是没有该功能的. @loadBalance是Netflix的ribbon中的一个负载均 ...

  9. 2.1 View与ViewGroup的概念

    http://www.runoob.com/w3cnote/android-tutorial-view-viewgroup-intro.html UI Overview 在Android APP中,所 ...

  10. 理解WebSocket心跳及重连机制(五)

    理解WebSocket心跳及重连机制 在使用websocket的过程中,有时候会遇到网络断开的情况,但是在网络断开的时候服务器端并没有触发onclose的事件.这样会有:服务器会继续向客户端发送多余的 ...