Leetcode546

给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分。

dp方程如下:

memo[l][r][k] = max(memo[l][r][k], dfs(boxes,memo,l,i,k+1) + dfs(boxes,memo,i+1,r-1,0));

意思是在序列的l-r部分后接k长度的 r值序列 所能得到的最大得分。

代码很简单

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

  

第十周 Leetcode 546. Remove Boxes (HARD) 记忆化搜索的更多相关文章

  1. Leetcode 546. Remove Boxes

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

  2. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

  3. UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)

     Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simple in one or t ...

  4. 546. Remove Boxes

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

  5. 546 Remove Boxes 移除盒子

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

  6. leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)

    https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...

  7. UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索

    题意:给出几个多维的箱子,如果箱子的每一边都小于另一个箱子的对应边,那就称这个箱子小于另一个箱子,然后要求能够套出的最多的箱子. 要注意的是关系图的构建,对箱子的边排序,如果分别都小于另一个箱子就说明 ...

  8. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

随机推荐

  1. Codeforces Beta Round #19D(Points)线段树

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  2. I - DFS(依然是漫水填充)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  3. 使用JS对form的内容验证失败后阻止提交

    1.form的两个事件 submit,提交表单,如果直接调用该函数,则直接提交表单 onSubmit,提交按钮点击时先触发,然后触发submit事件.如果不加控制的话,默认返回true,因此表单总能提 ...

  4. ABC077翻车实况

    今天强行打一波ABC,想作为信心赛,然而= = T1  日常练习读入&输出 T2  $STL$大法好,$sqrt$保平安,我强行递推$WA$了一圈,然后罚时++ T3  woc好难啊,$n=1 ...

  5. [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)

    传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...

  6. [K/3Cloud] 在设计时复制已有表单菜单或菜单项快速建立菜单

    1.打开已有表单XML,找到FormAppearance的Menu节点,按Ctrl+C复制 <Appearances> <FormAppearance ElementType=&qu ...

  7. idea使用之maven中央仓库索引更新

    接着上篇,上篇是更新本地已有的索引,这样在编写pom文件的时候,可以自动提示,但如果我们能够把整个中央仓库的索引更新下来,那不是更方便啦. 打开settings-->Build,Executio ...

  8. springMVC @Value的使用

    @Value 功能:将一个SpEL(SpEL:spring表达式类似于ognl)表达式结果映射到功能处理方法的参数上 例子:获取系统参数'java.vm.version'的值给到变量jvmVersio ...

  9. msp430入门编程06

    msp430中C语言的程序结构06 msp430中C语言的函数及实现07 msp430中C语言操作端口I/O10 msp430中C语言的模块化头文件及实现11 msp430中C语言的模块化头文件及库文 ...

  10. 基于端口的信息探测-portscan-1.0

    http://www.tiaozhanziwo.com/archives/174.html