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, k >= 1), remove them and get k*k points.
Find the maximum points you can get.

Example 1:
Input:

[1, 3, 2, 2, 2, 3, 4, 3, 1]

Output:

23

Explanation:

[1, 3, 2, 2, 2, 3, 4, 3, 1]
----> [1, 3, 3, 4, 3, 1] (3*3=9 points)
----> [1, 3, 3, 3, 1] (1*1=1 points)
----> [1, 1] (3*3=9 points)
----> [] (2*2=4 points)

Note: The number of boxes n would not exceed 100.

思路:https://leetcode.com/problems/remove-boxes/discuss/101310/Java-top-down-and-bottom-up-DP-solutions

方法一:Top-down, 记忆化搜索

 1 // top-down memorization dfs
 2 class Solution {
 3 public:
 4     int removeBoxes(vector<int>& boxes) {
 5         int len = boxes.size();
 6         int dp[100][100][100] = {0};
 7         return removeBoxes(boxes, dp, 0, len-1, 0);
 8     }
 9 private:
10     int removeBoxes(vector<int>& boxes,int dp[100][100][100],int i,int j,int k) {
11         if (i>j) {
12             return 0;
13         }
14         if (dp[i][j][k] > 0) {
15             return dp[i][j][k];
16         }
17         for (; i + 1 <= j && boxes[i + 1] == boxes[i]; i++, k++);
18         int res = removeBoxes(boxes, dp, i + 1, j, 0) + (k + 1) * (k + 1);
19         for (int m = i + 1; m <= j; m++) {
20             if (boxes[m] == boxes[i]) {
21                 res = max(res, removeBoxes(boxes, dp, i + 1, m - 1, 0) + removeBoxes(boxes, dp, m, j, k + 1));
22             }
23         }
24         dp[i][j][k] = res;
25         return dp[i][j][k];
26     }
27 };

方法二:bottom-up DP

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

原文地址:https://www.cnblogs.com/qinduanyinghua/p/11511041.html

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

leetcode 546. Remove 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] 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笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

LeetCode:Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

[LeetCode 题解]: Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题意:给定一个已排序的链表,删除其中重复的元素,保证每个元素有且仅有一个. 使用两个指针,分别指向前一个元素,和当前元素,

LeetCode OJ - Remove Nth Node From End of List

题目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Give

LeetCode:Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Solution: class Solution { public: int removeElement(int

[LeetCode] 027. Remove Element (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 027. Remove Element (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-element/ 代码(github):https://github.com/illuz/leetcode 题意: 删除一个数组里值为 elem 的所有数. 分析: 用