当前位置: 代码迷 >> 综合 >> LeetCode刷题:546. Remove Boxes —JAVA代码DP+DFS
  详细解决方案

LeetCode刷题:546. Remove Boxes —JAVA代码DP+DFS

热度:58   发布时间:2024-01-15 19:32:15.0

LeetCode刷题:546. Remove Boxes

原题链接:https://leetcode.com/problems/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<Integer> colors = new ArrayList<>();List<Integer> 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 1lens.set(lens.size() - 1, lens.get(lens.size() - 1) + 1);} else {// new colorcolors.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 answerint[][][] dp = new int[M][M][N];return dfs(colors, lens, 0, M - 1, 0, dp);}// top-down dfs search with memoizationprivate int dfs(List<Integer> colors, List<Integer> 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 - 1for (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] nowscore = 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 stubint[] 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~

  相关解决方案