动态规划——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 ...
随机推荐
- python小练习: 给定一个数组 按重复次数 降序排列输出 数组非空且为正整数
假设有个列表 a=[1,1,1,2,2,4,5,5,5,5] (非空且为正整数) 那么根据要求 最终输出的形式为 5,1,2,4 (按重复次数 降序排列输出) 代码实现及解释: a=[1,1,1 ...
- checkbox 实现互斥选择
// mutex 互斥 checkbox 互斥/** 互斥的原理.找到需要互斥的所有的元素.赋值 checked=false; 然后单独赋值 checked=true* */var mutexbox ...
- centos6 & centos 7 防火墙设置
转载:原文地址:http://blog.csdn.net/u011846257/article/details/54707864 Centos升级到7之后,内置的防火墙已经从iptables变成了fi ...
- AngularJs实现表单验证
首先,我们应该知道,表单中,常用的验证操作有: $dirty 表单有填写记录 $valid 字段内容合法的 $invalid 字段内容是非法的 $pristine 表单没有填写记录 $error 表单 ...
- QT背景颜色,菜单颜色更改
1.进入QT上置菜单栏 工具->选项 2.进入选项中 环境->interface (1)颜色:点击重置是默认颜色,想修改其他颜色,点击重置旁边的颜色自行选择. (2)Theme:这个里面提 ...
- Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes
题目链接 题意:给你一个长度n,还有2*n-2个字符串,长度相同的字符串一个数前缀一个是后缀,让你把每个串标一下是前缀还是后缀,输出任意解即可. 思路;因为不知道前缀还是后缀所以只能搜,但可以肯定的是 ...
- DC综合简单总结(2)
DC综合简单总结(2) 建立时间和保持时间和数据输出延时时间 一.概念 建立时间和保持时间都是针对触发器的特性说的. 建立时间(Tsu:set up time) 是指在触发器的时钟信号上升沿到来以前, ...
- Mac环境下Scrapy的安装
直接命令安装: $ easy_install scrapy 从 GitHub 安装: $ git clonehttps://github.com/scrapy/scrapy.git $ cd scra ...
- C++入门篇九
explicit关键字:防止构造函数隐式类型转换 #include <iostream> #include "pch.h" using namespace std; c ...
- 使用element-ui遇到的各种小问题
一.Dialog对话框 1.在使用嵌套Dialog的时候,会出现遮罩层在内容的上方这种错乱情况 解决办法:http://element-cn.eleme.io/#/zh-CN/component/di ...