动态规划——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 ...
随机推荐
- Contest2178 - 2019-4-18 高一noip基础知识点 测试7 题解版
刚刚改完题,才有时间发题解 传送门 T1 exgcd裸题 对a,b跑exgcd,答案就是x*c/gcd(a,b),y*c/gcd(a,b) 不合法的情况:当且仅当c%gcd(a,b)!=0 代码 T2 ...
- PC端判断浏览器类型及移动端判断移动设备类型
浏览器代理检测,可以检测出来用户使用的浏览器类型,也可以检测浏览器所在的操作系统 navigator.userAgent (1).判断浏览器类型 var t = navigator.userAgent ...
- Mac环境下Redis的安装
1.下载 官网下载地址:https://redis.io/download,选择对应的下载版本,我下载的是4.0.12 2.安装 1)下载文件解压后复制到/usr/local/目录下(快速找到路径小技 ...
- json中的json.dumps()
Json简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...
- 【easy】268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- linux中gcc和g++的区别
1.两者都是编译器 2.gcc编译c语言:g++既可以编译c语言,也可以编译c++语言 3.gcc不能自动链接库文件,一般用g++来链接库文件,非要用gcc的话,一般使用gcc -lstdc++命令 ...
- iptables系列
详情请参考:http://www.zsythink.net/archives/tag/iptables/page/2/
- form 组件
https://www.cnblogs.com/wupeiqi/articles/6144178.html class F2Form(forms.Form): title1=fields.CharFi ...
- 生活英语读写MOOC-Literature Tutor-有声名著阅读推荐
生活英语读写MOOC-Literature Tutor-有声名著阅读推荐 1. Alice's Adventures in Wonderland 爱丽丝漫游奇境记 音频与文本下载地址:链接:http: ...
- Centos 7部署docker
master安装: 安装zookeeper -openjdk java--openjdk-headless rpm -i packages/mesosphere-zookeeper--.centos7 ...