动态规划——Remove Boxes

很久没写博客了,越来越懒了,这次还是要分享LeetCode上一道动态规划的题目,和之前的Ballon Boom那个题(我记得是这个标题吧。。。)差不多,都是对一个数组的区间进行枚举的题,而且涉及到区间和子区间取值的问题,不过那个题和矩阵链乘法基本是一样的,

这个题的话相对来说更难一点,因为这个题需要对一个三维的dp数组进行维护,最后一个维度的考虑是比较难的。直接提供代码,思路以后有时间再补:

 1 class Solution {
 2     public int removeBoxes(int[] boxes) {
 3         if(boxes==null)return 0;
 4         int len = boxes.length;
 5         if(len<=1)return len*len;
 6         int[][][]dp = new int[len][len][len];
 7         for(int i = 0;i<len;i++)
 8             for(int j = 0;j<=i;j++)
 9                 dp[i][i][j] = (1+j)*(1+j);
10         int res = 0;
11         for(int i = len-2;i>=0;i--) {
12             for(int j = i+1;j<len;j++) {
13                 for(int k = 0;k<=i;k++) {
14                     res = (1+k)*(1+k)+dp[i+1][j][0];
15                     for(int m = i+1;m<=j;m++)
16                         if(boxes[i]==boxes[m])res = Math.max(res,dp[i+1][m-1][0]+dp[m][j][k+1]);
17                     dp[i][j][k] = res;
18                 }
19             }
20         }
21         return dp[0][len-1][0];
22     }
23 }

原文地址:https://www.cnblogs.com/messi2017/p/10055192.html

时间: 2024-10-08 16:03:42

动态规划——Remove Boxes的相关文章

[LeetCode] Remove Boxes 移除盒子

Given several boxes with different colors represented by different positive numbers. You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes

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

Given several boxes with different colors represented by different positive numbers. You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes

leetcode 546. Remove Boxes

Given several boxes with different colors represented by different positive numbers. You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes

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

Leetcode546 给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分. 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值序列 所能得到的最大得分. 代码很简单 class Solution { public: int removeBoxes(vector<int>&

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

codeforces 551 C GukiZ hates Boxes

--睡太晚了...脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最开始都站在第一个箱子的左边, 每一个人在每一秒钟都必须做出两种选择中的一种:1若他的位置有箱子则搬走一个箱子,2往右走一步. 问把所有箱子都搞掉的最少时间-- 很显然二分一下答案,若为x秒,则每个人都有x秒,一个一个排出去搬,看是否能够搬完-- 我竟然没想到-- #include<map> #include<string> #include<

Codeforces551C:GukiZ hates Boxes(二分+贪心)

Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1?≤?i?≤?n) containing ai boxes. Luckily, m stude

leetcode 564,546

564 Find the Closest Palindrome:给出一个长度不超过18的非负整数,求出与其最近接的回文整数(不包括它自己). 思路:假设其长度为偶数.将其分为两半,前一半设为$x$,那么最后的答案的前一部分一定是$x-1,x,x+1$三个中的一个.在这里,$x-1$有可能变成少一位的数字,比如100到99: $x+1$可能多一位,比如99到100.长度为奇数时类似的思路. long long strToInt(string s) { long long t=0; for(int

Autonomous driving - Car detection YOLO

Autonomous driving - Car detection Welcome to your week 3 programming assignment. You will learn about object detection using the very powerful YOLO model. Many of the ideas in this notebook are described in the two YOLO papers: Redmon et al., 2016 (