[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, 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.



给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色。
你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止。每一轮你可以移除具有相同颜色的连续 k 个盒子(k >= 1),这样一轮之后你将得到 k*k 个积分。
当你将所有盒子都去掉之后,求你能获得的最大积分和。

示例 1:
输入:

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

输出:

23

解释:

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


Runtime: 1784 ms

Memory Usage: 21.6 MB

 1 class Solution {
 2     func removeBoxes(_ boxes: [Int]) -> Int {
 3         var n:Int = boxes.count
 4         var dp = [[[Int]]](repeating: [[Int]](repeating: [Int](repeating: 0, count: n), count: n), count: n)
 5         for i in 0..<n
 6         {
 7             for k in 0...i
 8             {
 9                 dp[i][i][k] = (1 + k) * (1 + k)
10             }
11         }
12         for t in 1..<n
13         {
14             for j in t..<n
15             {
16                 var i:Int = j - t
17                 for k in 0...i
18                 {
19                     var res:Int = (1 + k) * (1 + k) + dp[i + 1][j][0]
20                     for m in (i + 1)...j
21                     {
22                         if boxes[m] == boxes[i]
23                         {
24                             res = max(res, dp[i + 1][m - 1][0] + dp[m][j][k + 1])
25                         }
26                     }
27                     dp[i][j][k] = res
28                 }
29             }
30         }
31         return n == 0 ? 0 : dp[0][n - 1][0]
32     }
33 }

原文地址:https://www.cnblogs.com/strengthen/p/10409173.html

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

[Swift]LeetCode546. 移除盒子 | 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

Leetcode 546.移除盒子

移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色.你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子(k >= 1),这样一轮之后你将得到 k*k 个积分.当你将所有盒子都去掉之后,求你能获得的最大积分和. 示例 1:输入: [1, 3, 2, 2, 2, 3, 4, 3, 1] 输出: 23 解释: [1, 3, 2, 2, 2, 3, 4, 3, 1] ----> [1, 3, 3, 4, 3, 1] (

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

[Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->

第十周 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>&

jQuery 元素移除empty() remove()与detach()的区别?

@1.empty() 删除匹配元素集合中所有的后代字节点元素: <p>hello<span>world</span></p> $("p").empty();   <p></p> @2.remove([expr]) 接收参数表示删除指定的元素 $("p").remove()  删除所有段落 $("p").remove(".hel") 删除带有.hel类名的段

动态规划——Remove Boxes

很久没写博客了,越来越懒了,这次还是要分享LeetCode上一道动态规划的题目,和之前的Ballon Boom那个题(我记得是这个标题吧...)差不多,都是对一个数组的区间进行枚举的题,而且涉及到区间和子区间取值的问题,不过那个题和矩阵链乘法基本是一样的, 这个题的话相对来说更难一点,因为这个题需要对一个三维的dp数组进行维护,最后一个维度的考虑是比较难的.直接提供代码,思路以后有时间再补: 1 class Solution { 2 public int removeBoxes(int[] bo

[Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcab

swift分布式存储多节点部署

1.机器 192.168.1.211    Proxy Node 192.168.1.212    Storage Node 192.168.1.213    Storage Node 192.168.1.214    Storage Node 系统为SLES11sp1 2.配置软件源 因为公司服务器无法连外网,所以配置局域网源和本地源来搭建环境 上传ISO镜像文件到各台机器 SLES-11-SP4-DVD-x86_64-GM-DVD1.iso 每台机器挂载镜像,配置本地源 # mkdir /m