modify code

pull/6/head
algorithmzuo 5 years ago
parent dc11e1605e
commit 3157e4000f

@ -1,5 +1,6 @@
package class46;
// 本题测试链接 : https://leetcode.com/problems/burst-balloons/
public class Code01_BurstBalloons {
public static int maxCoins1(int[] arr) {

@ -1,34 +1,26 @@
package class46;
// 本题测试链接 : https://leetcode.com/problems/remove-boxes/
public class Code02_RemoveBoxes {
public int removeBoxes(int[] boxes) {
public static int removeBoxes(int[] boxes) {
int N = boxes.length;
int[][][] dp = new int[N][N][N];
return process(boxes, 0, N - 1, 0, dp);
int ans = process(boxes, 0, N - 1, 0, dp);
return ans;
}
// boxes[L....R]前面还跟着K个boxes[L]
// 前面的包袱和L...R所有的数都消掉最好得分是什么
public static int process(int[] boxes, int L, int R, int K, int[][][] dp) {
if (L > R) {
return 0;
}
if (dp[L][R][K] != 0) {
return dp[L][R][K];
}
if (L == R) {
dp[L][R][K] = (K + 1) * (K + 1);
if (dp[L][R][K] > 0) {
return dp[L][R][K];
}
while (L < R && boxes[L] == boxes[L + 1]) {
L++;
K++;
}
int ans = (K + 1) * (K + 1) + process(boxes, L + 1, R, 0, dp);
for (int m = L + 1; m <= R; m++) {
if (boxes[L] == boxes[m]) {
ans = Math.max(ans, process(boxes, L + 1, m - 1, 0, dp) + process(boxes, m, R, K + 1, dp));
int ans = process(boxes, L, R - 1, 0, dp) + (K + 1) * (K + 1);
for (int i = L; i < R; i++) {
if (boxes[i] == boxes[R]) {
ans = Math.max(ans, process(boxes, i + 1, R - 1, 0, dp) + process(boxes, L, i, K + 1, dp));
}
}
dp[L][R][K] = ans;

@ -1,5 +1,6 @@
package class47;
// 本题测试链接 : https://leetcode.com/problems/strange-printer/
public class Code01_StrangePrinter {
public static int strangePrinter(String s) {
@ -16,12 +17,7 @@ public class Code01_StrangePrinter {
}
for (int L = N - 3; L >= 0; L--) {
for (int R = L + 2; R < N; R++) {
// L....R
dp[L][R] = R - L + 1;
// L...k-1 k...R
for (int k = L + 1; k <= R; k++) {
dp[L][R] = Math.min(dp[L][R], dp[L][k - 1] + dp[k][R] - (str[L] == str[k] ? 1 : 0));
}

Loading…
Cancel
Save