题目链接:

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的问题,具体的可以看推荐题解

代码

  1. int dp[101][101][101];
  2. class Solution {
  3. public:
  4. int dfs(vector<int>& nums,int l,int r,int k){
  5. if(l>r) return 0;
  6. if(l==r) return (k+1)*(k+1);
  7. if(dp[l][r][k]>=0) return dp[l][r][k];
  8. int ll=l,rr=r,kk=k;
  9. for(;l+1<=r&&nums[l]==nums[l+1];l++,k++);
  10. dp[l][r][k]=dfs(nums,l+1,r,0)+(k+1)*(k+1);
  11. for(int i=l+1;i<=r;i++){
  12. if(nums[i]==nums[l]){
  13. dp[l][r][k]=max(dp[l][r][k],dfs(nums,l+1,i-1,0)+dfs(nums,i,r,k+1));
  14. }
  15. }
  16. return dp[ll][rr][kk]=dp[l][r][k];
  17. }
  18. int removeBoxes(vector<int>& nums) {
  19. int n=nums.size();
  20. 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;
  21. return dfs(nums,0,n-1,0);
  22. }
  23. };

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. python免费发送短信

    pip install twilio from twilio.rest import Client # Your Account SID from twilio.com/console account ...

  2. Session共享的四种方法

    1. 基于NFS的Session共享 NFS是Net FileSystem的简称,最早由Sun公司为解决Unix网络主机间的目录共享而研发. 这个方案实现最为简单,无需做过多的二次开发,仅需将共享目录 ...

  3. File类_删除一个带内容的目录_练习

    需求:删除一个带内容的目录 原理:必须从最里面往外删除需要深度遍历 import java.io.File; public class RemoveDirTest { public static vo ...

  4. 20145203盖泽双 《网络对抗技术》实践九:Web安全基础实践

    20145203盖泽双 <网络对抗技术>实践九:Web安全基础实践 1.实践目标 1.理解常用网络攻击技术的基本原理. 2.Webgoat下进行相关实验:SQL注入攻击.XSS攻击.CSR ...

  5. 华为交换机常用命令(以s5700-SI为例)

    交换机的三种模式: Access模式: 一般用来连接计算机与交换机. 此模式下有一个PVID就是本端口所属的VLAN号,如果从链路上收到无标签的帧,则打上默认VLAN号,然后发给其他端口,如果从链路上 ...

  6. 崩 oj 1768 最大子矩阵

    描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵.比如,如下4 * 4的矩阵0 -2 -7  0 9  2 -6  2 -4  1 - ...

  7. 关于MySQL卸载重新安装的问题

    大体上一共分为3步,那么我们就美其名曰——三步走搞定MySQL安装 为什么说3步呢,如果你非要计较说我一次就重新安装成功了,就当我没说,这些是说给那些经常安装失败的同学看的! 切记,如若不想再以后My ...

  8. CTS 如何处理 gating clock 和 generated clock

    1. CTS 时会将 ICG cell 作为 implicit nostop pin 处理,直接穿透,以 ICG cell 后面的 sink 点作为真正的 sink 来长 tree 2. CTS 时会 ...

  9. Android 动态的给Button、TextView、ImageView等控件设置了background后,再设置padding属性时该属性不起作用

    也许大家遇到这样一个问题,有时我们根据业务需要在一个ViewGroup中动态的(程序运行过程中)添加View.例如添加Button,就需要给Button添加background.padding.mar ...

  10. coredns CrashLoopBackOff 报错

    1.kubectl logs -f coredns-99b9bb8bd-47mvf -n kube-system .:53 2018/09/22 07:39:37 [INFO] CoreDNS-1.2 ...