博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:546. Remove Boxes —JAVA代码DP+DFS
阅读量:4040 次
发布时间:2019-05-24

本文共 3036 字,大约阅读时间需要 10 分钟。

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.

给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色。

你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止。每一轮你可以移除具有相同颜色的连续 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 分)
 

提示:盒子的总数 n 不会超过 100。


算法设计

package com.bean.algorithm.basic;import java.util.ArrayList;import java.util.List;public class RemoveBoxesDemo {	public int removeBoxes(int[] boxes) {		List
colors = new ArrayList<>(); List
lens = new ArrayList<>(); // preprocessing // [1,1,1,3,3,2,3,3,3,1,1] would become // colors : [1,3,2,3,1] // lens : [3,2,1,3,2] for (int box : boxes) { if (!colors.isEmpty() && colors.get(colors.size() - 1) == box) { // continuous, increase length count by 1 lens.set(lens.size() - 1, lens.get(lens.size() - 1) + 1); } else { // new color colors.add(box); lens.add(1); } } int N = boxes.length; int M = colors.size(); // dp[i][j][k] means the maximal score for colors[i:j] with k boxes of same // color merged after j // i and j are inclusive, so dp[0][M - 1][0] will be the final answer int[][][] dp = new int[M][M][N]; return dfs(colors, lens, 0, M - 1, 0, dp); } // top-down dfs search with memoization private int dfs(List
colors, List
lens, int l, int r, int k, int[][][] dp) { if (l > r) return 0; if (dp[l][r][k] > 0) return dp[l][r][k]; // merging boxes with colors[r] int score = dfs(colors, lens, l, r - 1, 0, dp) + (lens.get(r) + k) * (lens.get(r) + k); // merge boxes with colors[l:i] and colors[l + 1:r - 1] where i from l to r - 1 for (int i = l; i < r; i++) { if (colors.get(i) == colors.get(r)) { // notice here : since we use top-down approach, colors[i + 1:r - 1] has already // been merged, so k = 0; // so color[i] is adjacent to color[r] now score = Math.max(score, dfs(colors, lens, l, i, lens.get(r) + k, dp) + dfs(colors, lens, i + 1, r - 1, 0, dp)); } } dp[l][r][k] = score; return score; } public static void main(String[] args) { // TODO Auto-generated method stub int[] boxes=new int[] {1, 3, 2, 2, 2, 3, 4, 3, 1}; RemoveBoxesDemo rbd=new RemoveBoxesDemo(); int result = rbd.removeBoxes(boxes); System.out.println("result = "+result); }}

程序运行结果:

result = 23

LeetCode上提交结果,Accepted~

转载地址:http://zntdi.baihongyu.com/

你可能感兴趣的文章
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>