546. Remove Boxes (HARD)
来源:互联网 发布:windows下nvidia smi 编辑:程序博客网 时间:2024/06/06 02:49
给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分。
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值序列 所能得到的最大得分。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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];
}
};
阅读全文
0 0
- 546. Remove Boxes (HARD)
- Leetcode546. Remove Boxes(Hard)
- 546. Remove Boxes
- leetcode 546. Remove Boxes
- [leetcode]546. Remove Boxes
- 546. Remove Boxes
- 546. Remove Boxes
- Remove Boxes
- [Leetcode] 546. Remove Boxes 解题报告
- LeetCode Week8: Remove boxes
- [LeetCode] Remove Boxes
- leetcode-Remove Boxes
- Leetcode546. Remove Boxes
- Leedcode 19 Remove Boxes
- leetcode 546. Remove Boxes 很复杂的DP
- Remove Antivirus XP Hard Disk Repair v9
- Hard-题目9:301. Remove Invalid Parentheses
- Remove Invalid Parentheses——Difficulty:Hard
- 欢迎使用CSDN-markdown编辑器
- Python画图工具类库matplotlib
- OOXX Game
- [ROR]Single Table Inheritance with Rails 4 (Part 1)
- 打印输出部门结构
- 546. Remove Boxes (HARD)
- code::block 常用快捷键 (最常用)
- UTMI+/ULPI
- CART回归树的构建
- 程序员面试题之计算阶乘n!尾数0的个数
- python learning2
- Android横竖屏解析
- 退出循环break
- 【ShawnZhang】带你看蓝桥杯—— 算法提高 阮小二买彩票