很久没写博客了,越来越懒了,这次还是要分享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的更多相关文章

  1. [LeetCode] Remove Boxes 移除盒子

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

  2. [Swift]LeetCode546. 移除盒子 | Remove Boxes

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

  3. 546. Remove Boxes

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

  4. 546 Remove Boxes 移除盒子

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

  5. 第十周 Leetcode 546. Remove Boxes (HARD) 记忆化搜索

    Leetcode546 给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分. dp方程如下: memo[l][r][k] = max(memo[l][r][k], dfs ...

  6. Leetcode 546. Remove Boxes

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

  7. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  8. 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/ ...

  9. 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 ...

随机推荐

  1. VS注释快捷键

    注释:        先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U 代码自动对齐:1, ctrl+a 2, ctrl+k 3, ctrl+f

  2. rcu-bp关键代码解读

    1      什么是TLS 原理在网上资料很多,这里不展开. 简单点说,动态申请的每线程变量.有一类比较熟悉的每线程变量是一个带__thread的每线程变量,两者的区别在于,TLS这类每线程变量是动态 ...

  3. 路径规划算法之Bellman-Ford算法

    最近由于工作需要一直在研究Bellman-Ford算法,这也是我第一次用C++编写代码. 1.Bellman-Ford算法总结 (1)Bellman-Ford算法计算从源点(起始点)到任意一点的最短路 ...

  4. Linux 默认日志类型

    nginx 默认日志类型有两个  access.log  http 记录访问日志. error.log    server 操作记录日志

  5. 前端node.js npm i 报错Unexpected end of JSON input while parsing near

    清缓存 npm cache clean --force 重新安装 npm install

  6. 清北学堂学习总结day2

    今天是钟皓曦大佬讲课,先来膜一波   %%%%% •数论 数论是这次培训的一个重点,那么什么是数论呢? 数论是研究整数性质的东西,所以理论上day2不会涉及小数QwQ (切入正题) •整除性: 设a, ...

  7. 【转】Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1

    转自:https://blog.csdn.net/AckClinkz/article/details/78538462 环境 >>> import sys >>> ...

  8. Linux Input子系统浅析(二)-- 模拟tp上报键值【转】

    转自:https://blog.csdn.net/xiaopangzi313/article/details/52383226 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

  9. zabbix监控ssl证书到期时间

    监控脚本 cat ssl_check.sh #!/bin/bash # #获取ssl证书的过期时间 #menghao #获取证书的有效时间 time=$(echo | openssl s_client ...

  10. docker安装小笔记

    作者:邓聪聪 yum update Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker docker卸载旧版本(如 ...