动态规划——Remove Boxes
很久没写博客了,越来越懒了,这次还是要分享LeetCode上一道动态规划的题目,和之前的Ballon Boom那个题(我记得是这个标题吧。。。)差不多,都是对一个数组的区间进行枚举的题,而且涉及到区间和子区间取值的问题,不过那个题和矩阵链乘法基本是一样的,
这个题的话相对来说更难一点,因为这个题需要对一个三维的dp数组进行维护,最后一个维度的考虑是比较难的。直接提供代码,思路以后有时间再补:
class Solution {
public int removeBoxes(int[] boxes) {
if(boxes==null)return 0;
int len = boxes.length;
if(len<=1)return len*len;
int[][][]dp = new int[len][len][len];
for(int i = 0;i<len;i++)
for(int j = 0;j<=i;j++)
dp[i][i][j] = (1+j)*(1+j);
int res = 0;
for(int i = len-2;i>=0;i--) {
for(int j = i+1;j<len;j++) {
for(int k = 0;k<=i;k++) {
res = (1+k)*(1+k)+dp[i+1][j][0];
for(int m = i+1;m<=j;m++)
if(boxes[i]==boxes[m])res = Math.max(res,dp[i+1][m-1][0]+dp[m][j][k+1]);
dp[i][j][k] = res;
}
}
}
return dp[0][len-1][0];
}
}
动态规划——Remove Boxes的更多相关文章
- [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 ...
- 546. Remove Boxes
Given several boxes with different colors represented by different positive numbers. You may experie ...
- 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动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...
- Codeforces821C Okabe and Boxes 2017-06-28 15:24 35人阅读 评论(0) 收藏
C. Okabe and Boxes time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- H5_0008:链接分享图片和判断平台
<!--分享图片--><div id="share_img" style="display:none;"><img class=& ...
- 对半导体制造(FAB)工种的全方位解析
本文转载自微信公众号 - 感集网, 链接 https://mp.weixin.qq.com/s/MRoWRbKZFBrJcQAZPqDa7w
- WebGL&Three.js工作原理
一.我们讲什么? 我们讲两个东西:1.WebGL背后的工作原理是什么?2.以Three.js为例,讲述框架在背后扮演什么样的角色? 二.我们为什么要了解原理? 我们假定你对WebGL已经有一定了解,或 ...
- EF提交插入数据catch捕获具体异常方法
try { db.SaveChanges(); } catch (DbEntityValidationException ex) { StringBuilder errors = new String ...
- Python-re模块中一些重要函数
re模块包含对正则表达式的支持.
- Fastjson反序列化漏洞研究
0x01 Brief Description java处理JSON数据有三个比较流行的类库,gson(google维护).jackson.以及今天的主角fastjson,fastjson是阿里巴巴一个 ...
- java多线程基础篇第一篇
1.在开始多线程之前,我们先来聊聊计算机的缓存 计算机处理一个程序需要cpu处理器与存储设备的交互.但是在计算机发展的过程中,cpu处理器的处理速度不断提高,而存储设备的读写速度却没有得到与cpu同样 ...
- sql的四种匹配模式
1. % 表示任意0个或多个字符.如下语句:Select * FROM user Where name LIKE '%三%'; 将会把name为“张三”,“三脚猫”,“唐三藏”等等有“三”的全找出来. ...
- C++自己实现一个String类
C++自己实现一个String类(构造函数.拷贝构造函数.析构函数和字符串赋值函数) #include <iostream> #include <cstring> using ...
- C# 断言 Assert
重构-断言 现象:某一段代码需要对程序状态做出某种假设 做法:以断言明确表现这种假设 动机: 常常有这种一段代码:只有某个条件为真是,该改名才能正常运行. 通常假设这样的假设并没有代码中明确表现出来, ...