546. Remove Boxes

网友投稿 611 2022-09-04

546. Remove Boxes

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----> [1, 3, 3, 3, 1] (1*1=1----> [1, 1] (3*3=9----> [] (2*2=4

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

class Solution { 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]); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:磁盘 IO 和网络 IO 的评估、监控与调优知识总结(磁盘空间不足是什么意思)
下一篇:587. Erect the Fence
相关文章

 发表评论

暂时没有评论,来抢沙发吧~