546. Remove Boxes
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的更多相关文章
- 546 Remove Boxes 移除盒子
给定一些不同颜色的盒子,以不同的正整数表示.消去连续相同颜色的盒子,直到全部消除完毕为止.每一次消去可以得到k * k分(k为消去盒子的个数, k >= 1).计算可以得到的最大得分.注意:盒 ...
- 第十周 Leetcode 546. Remove Boxes (HARD) 记忆化搜索
Leetcode546 给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分. dp方程如下: memo[l][r][k] = max(memo[l][r][k], dfs ...
- Leetcode 546. Remove Boxes
题目链接: https://leetcode.com/problems/remove-boxes/description/ 问题描述 若干个有序排列的box和它们的颜色,每次可以移除若干个连续的颜色相 ...
- [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 ...
- 动态规划——Remove Boxes
很久没写博客了,越来越懒了,这次还是要分享LeetCode上一道动态规划的题目,和之前的Ballon Boom那个题(我记得是这个标题吧...)差不多,都是对一个数组的区间进行枚举的题,而且涉及到区间 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- LeetCode Weekly Contest 25
1. 507. Perfect Number 显然小于2的都不满足(尤其是负数的情况),进一步,显然质数都不满足,所以小于4的数,直接return false. 然后依次暴力枚举判断到sqrt(n), ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
随机推荐
- 使用正则表达式读取简单的xml文件
'<?xml version='1.0' encoding='GB2312'?>'<ntsc>' <time>' <year>2010& ...
- 迷你MVVM框架 avalonjs 0.97发布
在本版本中,王之三柱臣全部就位! mmRouter: https://github.com/RubyLouvre/mmRouter mmAnimate: https://github.com/Ruby ...
- 获取select值及判断是否是数字
代码片段 <div class="container-fluid"> <div class="row"> <div class=& ...
- 【Git】三、工作区、暂存区、版本库
一.基础概念 工作区:电脑中可以看到的目录,为电脑中的项目文件 暂存区:暂存修改的地方 版本库:存放项目的各个版本文件 二.详细介绍 工作区为我们工作所使用的目录,在工作区我们对项目文件进行增删改查. ...
- grep 过滤.svn文件
[grep 过滤.svn文件] 问题: 在repository搜索代码时,常常会搜索到.svn的代码,如果不想搜索.svn目录下的相关代码怎么办? 1.使用管道进行双层“过滤”,其中第二次gre ...
- tar使用
[tar使用] 1..tar.gz文件 压缩:tar -czvf dstFileName.tar.gz a.txt b.txt …… 解压:tar -xzvf fileName.tar.gz 2..t ...
- 阿里云自定义监控tomcat进程数
阿里云提供自定义监控SDK,这有助于我们定制化的根据自身业务来做监控,下面我就根据业务需求来介绍一个简单的自定义监控配置. 阿里提供了2个版本的自定义监控接口:自定义监控SDK(python版) :c ...
- 运行Junit单测时遇到的问题
现在有两个办法解决: 1.junit版本降到4.10 2.导入hamcrest-core-1.3.jar 官网:JUnit now uses the latest version of Hamcres ...
- linux下PHP7安装memcache
1.memcache服务器的安装 .分别把memcached和libevent下载回来,放到 /tmp 目录下: # cd /tmp # wget http://www.danga.com/memca ...
- [Training Video - 2] [Groovy Introduction]
Building test suites, Test cases and Test steps in SOAP UI Levels : test step level test case level ...