Skip to content

Commit 3157e40

Browse files
committed
modify code
1 parent dc11e16 commit 3157e40

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

src/class46/Code01_BurstBalloons.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package class46;
22

3+
// 本题测试链接 : https://leetcode.com/problems/burst-balloons/
34
public class Code01_BurstBalloons {
45

56
public static int maxCoins1(int[] arr) {

src/class46/Code02_RemoveBoxes.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
package class46;
22

3+
// 本题测试链接 : https://leetcode.com/problems/remove-boxes/
34
public class Code02_RemoveBoxes {
45

5-
public int removeBoxes(int[] boxes) {
6+
public static int removeBoxes(int[] boxes) {
67
int N = boxes.length;
78
int[][][] dp = new int[N][N][N];
8-
return process(boxes, 0, N - 1, 0, dp);
9+
int ans = process(boxes, 0, N - 1, 0, dp);
10+
return ans;
911
}
1012

11-
// boxes[L....R],前面还跟着K个boxes[L]
12-
// 前面的包袱和L...R所有的数都消掉,最好得分是什么
1313
public static int process(int[] boxes, int L, int R, int K, int[][][] dp) {
1414
if (L > R) {
1515
return 0;
1616
}
17-
if (dp[L][R][K] != 0) {
18-
return dp[L][R][K];
19-
}
20-
if (L == R) {
21-
dp[L][R][K] = (K + 1) * (K + 1);
17+
if (dp[L][R][K] > 0) {
2218
return dp[L][R][K];
2319
}
24-
while (L < R && boxes[L] == boxes[L + 1]) {
25-
L++;
26-
K++;
27-
}
28-
int ans = (K + 1) * (K + 1) + process(boxes, L + 1, R, 0, dp);
29-
for (int m = L + 1; m <= R; m++) {
30-
if (boxes[L] == boxes[m]) {
31-
ans = Math.max(ans, process(boxes, L + 1, m - 1, 0, dp) + process(boxes, m, R, K + 1, dp));
20+
int ans = process(boxes, L, R - 1, 0, dp) + (K + 1) * (K + 1);
21+
for (int i = L; i < R; i++) {
22+
if (boxes[i] == boxes[R]) {
23+
ans = Math.max(ans, process(boxes, i + 1, R - 1, 0, dp) + process(boxes, L, i, K + 1, dp));
3224
}
3325
}
3426
dp[L][R][K] = ans;

src/class47/Code01_StrangePrinter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package class47;
22

3+
// 本题测试链接 : https://leetcode.com/problems/strange-printer/
34
public class Code01_StrangePrinter {
45

56
public static int strangePrinter(String s) {
@@ -16,12 +17,7 @@ public static int strangePrinter(String s) {
1617
}
1718
for (int L = N - 3; L >= 0; L--) {
1819
for (int R = L + 2; R < N; R++) {
19-
20-
// L....R
21-
2220
dp[L][R] = R - L + 1;
23-
24-
// L...k-1 k...R
2521
for (int k = L + 1; k <= R; k++) {
2622
dp[L][R] = Math.min(dp[L][R], dp[L][k - 1] + dp[k][R] - (str[L] == str[k] ? 1 : 0));
2723
}

0 commit comments

Comments
 (0)