Given several boxes with different colors represented by different positive numbers. 
You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes, k >= 1), remove them and get k*k points.
Find the maximum points you can get.

Example 1:
Input:

[1, 3, 2, 2, 2, 3, 4, 3, 1]

Output:

23

Explanation:

[1, 3, 2, 2, 2, 3, 4, 3, 1]
----> [1, 3, 3, 4, 3, 1] (3*3=9 points)
----> [1, 3, 3, 3, 1] (1*1=1 points)
----> [1, 1] (3*3=9 points)
----> [] (2*2=4 points)

Approach #1: three dimensional DP. [C++] [Hard]

class Solution {
public:
int removeBoxes(vector<int>& boxes) {
int n = boxes.size();
m_ = vector<vector<vector<int>>>(n, vector<vector<int>>(n, vector<int>(n, 0)));
return dfs(boxes, 0, n-1, 0);
} private:
vector<vector<vector<int>>> m_; int dfs(const vector<int>& boxes, int l, int r, int k) {
if (l > r) return 0;
if (m_[l][r][k] != 0) return m_[l][r][k];
while (l < r && boxes[r-1] == boxes[r]) {--r; ++k;}
m_[l][r][k] = dfs(boxes, l, r-1, 0) + (k + 1) * (k + 1);
for (int i = l; i < r; ++i) {
if (boxes[i] == boxes[r])
m_[l][r][k] = max(m_[l][r][k], dfs(boxes, l, i, k + 1) + dfs(boxes, i + 1, r - 1, 0));
}
return m_[l][r][k];
}
};

  

Analysis:

dp[i][j][k] = max score of subarray b[i] ~ b[j] if there are k boxes that have the same color as b[j] following b[j]. Those k boxes are from box[j+1] ~ box[n], to simulate boxes with other colors are removed first.

Transition:

dp[i][j][k] = dp[i][j-1][0] + (k + 1)^2    # case 1

       dp[i][p][k+1] + dp[p+1][j-1][0]  # case 2

Case 1: drop box[j], remove k + 1 boxes.

Case 2: Try all breakpoints p, attach a[j] to a[p], i <= p < j, box[p] == box[j].

Ans:

dp[0][n-1][0]

Tim complexity: O(n^4) Space complexity: O(n^3)

Reference:

https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-546-remove-boxes/

546. Remove Boxes的更多相关文章

  1. 546 Remove Boxes 移除盒子

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

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

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

  3. Leetcode 546. Remove Boxes

    题目链接: https://leetcode.com/problems/remove-boxes/description/ 问题描述 若干个有序排列的box和它们的颜色,每次可以移除若干个连续的颜色相 ...

  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. 动态规划——Remove Boxes

    很久没写博客了,越来越懒了,这次还是要分享LeetCode上一道动态规划的题目,和之前的Ballon Boom那个题(我记得是这个标题吧...)差不多,都是对一个数组的区间进行枚举的题,而且涉及到区间 ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. LeetCode Weekly Contest 25

    1. 507. Perfect Number 显然小于2的都不满足(尤其是负数的情况),进一步,显然质数都不满足,所以小于4的数,直接return false. 然后依次暴力枚举判断到sqrt(n), ...

  9. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

随机推荐

  1. eclipse liquibase 插件

    http://marketplace.eclipse.org/category/free-tagging/liquibase http://marketplace.eclipse.org/market ...

  2. Grapher - Change how graphs look

    [Change how graphs look] Pan and zoom the graph 工具条如下: Change the type of graph 开场的Graph Template在Me ...

  3. 2015年传智播客JavaEE 第168期就业班视频教程06-权限校验子系统介绍

    没整过论坛你也整过淘宝,其实淘宝登录的也分商家和个人,卖家和买家,不同的人登录显示的东西是不一样的.权限系统要分两大过程,第四天上午下午分开,分为授权与校验.我把某一个职务给你叫做授权,例如封你为征西 ...

  4. [Excel]拾取杂志图表的配色

    ColorPix是一款绿色的小软件,可以取到杂志图表所用到的颜色. 下载地址: http://www.colorschemer.com/colorpix_info.php

  5. [原创]分享本人自己PY写的BOOST编译程序(源码)

    本程序WINDOWS专用,只做抛砖引玉,希望诸位按照各自需求自行修改,主要目的是为了让诸位编译时可以省一些组合指令的时间,只需要修改几个参数即可自动编译. 支持64位编译模式. 改进版本:http:/ ...

  6. C#进阶系列——WebApi 异常处理解决方案(转)

    出处:http://www.cnblogs.com/landeanfen/p/5363846.html 阅读目录 一.使用异常筛选器捕获所有异常 二.HttpResponseException自定义异 ...

  7. 学习C++的50条忠告

    1. 把C++当成一门新的语言学习: 2. 看<Thinking In C++>,不要看<C++变成死相>: 3. 看<The C++ Programming Langu ...

  8. LVS初步

    LVS初步 一见 目录 目录 1 1. 前言 2 2. 思考 2 3. 名词解释 2 4. OSI参考模型 3 5. LVS架构 4 5.1. 负载均衡器(Load Balancer) 4 5.2.  ...

  9. require的路径问题(比较重要)

    dojo.baseUrl baseUrl用来存储dojo.js存放 的跟目录,例如dojo.js的路径是“/web/scripts/dojo-1.3/dojo/dojo.js”则baseUrl为“/w ...

  10. iOS9 视频播放

       self.videoFileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", self ...