leetcode 546. Remove Boxes
Solution 讲解: leetcode 546. Remove Boxes
很巧妙的分析思路:T(i, j, k) 来表示 k个相同前缀,i-j 之间的最大值
自己写的 自底向上的 Bottom-Top
public class Solution {public int removeBoxes(int[] boxes) {int len = boxes.length;if(len==0) return 0;int[][][] dp = new int[len][len][len+1];for(int inter=0;inter<len;inter++){for(int i=0;i+inter<len;i++){for(int k=0;k<len;k++){int j = i+inter; //j>=i// if j<i => 0dp[i][j][k] = 0;if(i==j) {dp[i][j][k]=(k+1)*(k+1);continue;}if(dp[i+1][j][0]+(k+1)*(k+1)>dp[i][j][k]){dp[i][j][k] = dp[i+1][j][0]+(k+1)*(k+1);}for(int h=i+1;h<=j;h++){if(boxes[h]==boxes[i]){if(dp[i+1][h-1][0] + dp[h][j][k+1]>dp[i][j][k]){dp[i][j][k] = dp[i+1][h-1][0] + dp[h][j][k+1];}}}}}}return dp[0][len-1][0];}
}
这一题很经典的dp题目,Solution给出如下的写法,也是dp常见的套路:
Top-Bottom:
public int removeBoxes(int[] boxes) {int n = boxes.length;int[][][] dp = new int[n][n][n];return removeBoxesSub(boxes, 0, n - 1, 0, dp);
}private int removeBoxesSub(int[] boxes, int i, int j, int k, int[][][] dp) {if (i > j) return 0;if (dp[i][j][k] > 0) return dp[i][j][k];int res = (k + 1) * (k + 1) + removeBoxesSub(boxes, i + 1, j, 0, dp);for (int m = i + 1; m <= j; m++) {if (boxes[i] == boxes[m]) {res = Math.max(res, removeBoxesSub(boxes, i + 1, m - 1, 0, dp) + removeBoxesSub(boxes, m, j, k + 1, dp));}}dp[i][j][k] = res;return res;
}
Bottom-Top:
public int removeBoxes(int[] boxes) {int n = boxes.length;int[][][] dp = new int[n][n][n];for (int j = 0; j < n; j++) {for (int k = 0; k <= j; k++) {dp[j][j][k] = (k + 1) * (k + 1);}}for (int l = 1; l < n; l++) {for (int j = l; j < n; j++) {int i = j - l;for (int k = 0; k <= i; k++) {int res = (k + 1) * (k + 1) + dp[i + 1][j][0];for (int m = i + 1; m <= j; m++) {if (boxes[m] == boxes[i]) {res = Math.max(res, dp[i + 1][m - 1][0] + dp[m][j][k + 1]);}}dp[i][j][k] = res;}}}return (n == 0 ? 0 : dp[0][n - 1][0]);
}
leetcode 546. Remove Boxes相关推荐
- 546. Remove Boxes 移除盒子
给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色. 你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子(k >= 1) ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [leetcode]83.Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- LeetCode:Remove Nth Node From End of List 移除链表倒第n项
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...
- LeetCode 19. Remove Nth Node From End of List
LeetCode 19. Remove Nth Node From End of List Solution1:我的答案 并不算是最优解法. /*** Definition for singly-li ...
- 数据结构与算法 | Leetcode 19. Remove Nth Node From End of List
原文链接:https://wangwei.one/posts/jav... 前面,我们实现了 两个有序链表的合并 操作,本篇来聊聊,如何删除一个链表的倒数第N个节点. 删除单链表倒数第N个节点 Lee ...
- [勇者闯LeetCode] 83. Remove Duplicates from Sorted List
[勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...
- leetcode 546. 移除盒子 —— 动态规划
将上面记忆化存储的递归算法,改为递推算法.即动态规划法. 546. 移除盒子 题目: 546. 移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色. 你将经过若干轮操作 ...
- LeetCode 316. Remove Duplicate Letters--贪心--Java,C++,Python解法
题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...
最新文章
- 高频数据交换下Flutter与ReactNative的对比
- Error dialog box generic entry point
- .NET Core 3.0愈加成熟,微软将不再把.NET Framework API移植给它
- NLP复习资料(2)-三~五章:形式语言、语料库、语言模型
- 工作流实战_12_flowable 流程实例 终止流程
- 查找路径php.ini文件到底在哪里?
- C# 用正则表达式替换字符串中所有特殊字符
- 2.1 全连接神经网络
- sql server 触发器
- CVE-2020-11946 ManageEngine OpManager 命令执行
- 抖音直播视频下载保存到本地地瓜网络技术
- 利用资源文件实现对软件的保护
- dede在添加文章页增加附件上传后点击浏览找不到文件提示No Exsits Path解决
- php取雅加达时间,2018雅加达亚运会赛程表完整版
- 控制台调出Servers
- 随便学学Python-day7-字典和集合
- “相濡以沫,不如相忘于江湖”
- iOS iPhone、iPad、Mac等禁止系统摄像头功能
- linux 下安装apache 快速教程
- 小型便携式AIS接收机dAI01
热门文章
- QtChart——简单的动态波形图
- System.ArgumentOutOfRangeException: “DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中
- 量化交易资源汇总《1》
- IGT-SER通过PLC和触摸屏的通讯口采集设备数据,上报到SQL数据库
- 视频播放不了?如何修复视频文件?
- 【ASP.NET Identity系列教程(三)】Identity高级技术
- IE11 RTM版不支持Uint8ClampedArray
- Java之多线程详解
- 快嘴阿里旺旺超强营销王 2007 绿色
- ViewPager设置焦点的问题