546 Remove Boxes 移除盒子
给定一些不同颜色的盒子,以不同的正整数表示。
消去连续相同颜色的盒子,直到全部消除完毕为止。每一次消去可以得到k * k分(k为消去盒子的个数, k >= 1)。
计算可以得到的最大得分。
注意:盒子的数量n不超过100。
详见:https://leetcode.com/problems/remove-boxes/description/
C++:
class Solution {
public:
int removeBoxes(vector<int>& boxes)
{
int n = boxes.size();
int dp[100][100][100] = {0};
return helper(boxes, 0, n - 1, 0, dp);
}
int helper(vector<int>& boxes, int i, int j, int k, int dp[100][100][100])
{
if (j < i)
{
return 0;
}
if (dp[i][j][k] > 0)
{
return dp[i][j][k];
}
int res = (1 + k) * (1 + k) + helper(boxes, i + 1, j, 0, dp);
for (int m = i + 1; m <= j; ++m)
{
if (boxes[m] == boxes[i])
{
res = max(res, helper(boxes, i + 1, m - 1, 0, dp) + helper(boxes, m, j, k + 1, dp));
}
}
return dp[i][j][k] = res;
}
};
参考:http://www.cnblogs.com/grandyang/p/6850657.html
546 Remove Boxes 移除盒子的更多相关文章
- [LeetCode] Remove Boxes 移除盒子
Given several boxes with different colors represented by different positive numbers. You may experie ...
- 546. Remove Boxes
Given several boxes with different colors represented by different positive numbers. You may experie ...
- Leetcode 546. Remove Boxes
题目链接: https://leetcode.com/problems/remove-boxes/description/ 问题描述 若干个有序排列的box和它们的颜色,每次可以移除若干个连续的颜色相 ...
- 第十周 Leetcode 546. Remove Boxes (HARD) 记忆化搜索
Leetcode546 给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分. dp方程如下: memo[l][r][k] = max(memo[l][r][k], dfs ...
- [Swift]LeetCode546. 移除盒子 | Remove Boxes
Given several boxes with different colors represented by different positive numbers. You may experie ...
- Java实现 LeetCode 546 移除盒子(递归,vivo秋招)
546. 移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色. 你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子( ...
- Leetcode 546.移除盒子
移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色.你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子(k > ...
- 【转载】C#通过Remove方法移除DataTable中的某一列数据
在C#中的Datatable数据变量的操作过程中,有时候我们需要移除当前DataTable变量中的某一列的数据,此时我们就需要使用到DataTable变量内部的Columns属性变量的Remove方法 ...
- 【转载】C#中List集合使用Remove方法移除指定的对象
在C#的List集合操作中,有时候需要将特定的对象或者元素移除出List集合序列中,此时可使用到List集合的Remove方法,Remove方法的方法签名为bool Remove(T item),it ...
随机推荐
- ansible快速学习
推荐文献: 表述的很不错, http://www.mamicode.com/info-detail-1428476.html 附加参考: http://laowafang.blog.51cto.com ...
- BestCoder7 1002 Little Pony and Alohomora Part I(hdu 4986) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4986 题目意思:有 n 个box(从左到右编号依次为1~n),每个box里面有一个随机的钥匙,有可能这 ...
- codeforces B. Trees in a Row 解题报告
题目链接:http://codeforces.com/problemset/problem/402/B 题目意思:给出n个数和公差k,问如何调整使得ai + 1 - ai = k.(1 ≤ i < ...
- CI 模型公用查询函数
/** * 多字段条件查询数据 * @param array $val array("name" => $value).name为要操作的字段,value为要操作的值 * @ ...
- Yii 表单验证规则---总结
Filter: 过滤,'filter'=>'trim',表示去空格 Required:必须的,表示不能为空 Match: 匹配正则,需要和pattern一起使用,定义正则表达式,'pattern ...
- [BZOJ 1475] 方格取数
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1475 [算法] 首先将方格黑白染色 , 也就是说 , 如果(i + j)为奇数 , ...
- C/C++获取操作系统、CPU、内存信息(windows和linux)
有时候需要在工程里面获取一些系统或者硬件的信息,比如系统版本,cpu,内存,显卡,硬盘等,作为后续软件功能判断的依据,甚至参与性能算法自适应建模 Windows 操作系统和内存信息在windows下通 ...
- Gulp简单应用
1.创建一个工程,在webstorm控制台 cnpm install --save-dev gulp cnpm install --save-dev gulp-concat ...
- 【旧文章搬运】加载PE文件时IAT的填充时机
原文发表于百度空间,2011-06-20========================================================================== 大致过程如 ...
- js中实现子页面向父页面中赋值
(方法一) 父页面: <input id="input1" type="text"/> <a href="javascript:wi ...