Skip to content

Commit 66a3a39

Browse files
add 546
1 parent 6a92322 commit 66a3a39

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Your ideas/fixes/algorithms are more than welcome!
5555
|549|[Binary Tree Longest Consecutive Sequence II](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BinaryTreeLongestConsecutiveSequenceII.java) | O(n) |O(n) | Medium | Tree
5656
|548|[Split Array with Equal Sum](https://leetcode.com/problems/split-array-with-equal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/SplitArraywithEqualSum.java) | O(n^2) |O(1) | Medium | Array
5757
|547|[Friend Circles](https://leetcode.com/problems/friend-circles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FriendCircles.java) | O(n^2) |O(n) | Medium | Union Find
58+
|546|[Remove Boxes](https://leetcode.com/problems/remove-boxes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_546.java) | O(n^3) |O(n^3) | Hard| DFS, DP
5859
|545|[Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BoundaryofBinaryTree.java) | O(n) |O(n) | Medium | Recursion
5960
|544|[Output Contest Matches](https://leetcode.com/problems/output-contest-matches/)|[Solution](../master/src/main/java/com/fishercoder/solutions/OutputContestMatches.java) | O(n) |O(n) | Medium | Recursion
6061
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/DiameterofBinaryTree.java) | O(n) |O(h) | Easy | Tree/DFS/Recursion
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 546. Remove Boxes
5+
*
6+
* Given several boxes with different colors represented by different positive numbers.
7+
You may experience several rounds to remove boxes until there is no box left.
8+
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.
9+
Find the maximum points you can get.
10+
11+
Example 1:
12+
Input:
13+
14+
[1, 3, 2, 2, 2, 3, 4, 3, 1]
15+
Output:
16+
23
17+
18+
Explanation:
19+
[1, 3, 2, 2, 2, 3, 4, 3, 1]
20+
----> [1, 3, 3, 4, 3, 1] (3*3=9 points)
21+
----> [1, 3, 3, 3, 1] (1*1=1 points)
22+
----> [1, 1] (3*3=9 points)
23+
----> [] (2*2=4 points)
24+
Note: The number of boxes n would not exceed 100.
25+
*/
26+
public class _546 {
27+
/**credit: https://leetcode.com/articles/remove-boxes/#approach-2-using-dp-with-memorizationaccepted*/
28+
public int removeBoxes(int[] boxes) {
29+
int[][][] dp = new int[100][100][100];
30+
return calculatePoints(boxes, dp, 0, boxes.length - 1, 0);
31+
}
32+
33+
public int calculatePoints(int[] boxes, int[][][] dp, int l, int r, int k) {
34+
if (l > r) return 0;
35+
if (dp[l][r][k] != 0) return dp[l][r][k];
36+
while (r > l && boxes[r] == boxes[r - 1]) {
37+
r--;
38+
k++;
39+
}
40+
dp[l][r][k] = calculatePoints(boxes, dp, l, r - 1, 0) + (k + 1) * (k + 1);
41+
for (int i = l; i < r; i++) {
42+
if (boxes[i] == boxes[r]) {
43+
dp[l][r][k] = Math.max(dp[l][r][k], calculatePoints(boxes, dp, l, i, k + 1) + calculatePoints(boxes, dp, i + 1, r - 1, 0));
44+
}
45+
}
46+
return dp[l][r][k];
47+
}
48+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy