modify code

pull/6/head
algorithmzuo 5 years ago
parent 07b1377e63
commit 85abdaa11f

@ -56,23 +56,15 @@ public class Code01_BurstBalloons {
} }
for (int L = N; L >= 1; L--) { for (int L = N; L >= 1; L--) {
for (int R = L + 1; R <= N; R++) { for (int R = L + 1; R <= N; R++) {
int finalL = help[L - 1] * help[L] * help[R + 1] + dp[L + 1][R]; int ans = help[L - 1] * help[L] * help[R + 1] + dp[L + 1][R];
int finalR = help[L - 1] * help[R] * help[R + 1] + dp[L][R - 1]; ans = Math.max(ans, help[L - 1] * help[R] * help[R + 1] + dp[L][R - 1]);
int max = Math.max(finalL, finalR); for (int i = L + 1; i < R; i++) {
for (int leftEnd = L + 1; leftEnd < R; leftEnd++) { ans = Math.max(ans, help[L - 1] * help[i] * help[R + 1] + dp[L][i - 1] + dp[i + 1][R]);
max = Math.max(max,
help[L - 1] * help[leftEnd] * help[R + 1] + dp[L][leftEnd - 1] + dp[leftEnd + 1][R]);
} }
dp[L][R] = max; dp[L][R] = ans;
} }
} }
return dp[1][N]; return dp[1][N];
} }
public static void main(String[] args) {
int[] arr = { 4, 2, 3, 5, 1, 6 };
System.out.println(maxCoins1(arr));
System.out.println(maxCoins2(arr));
}
} }

@ -36,18 +36,17 @@ public class Code02_RemoveBoxes {
public static int process2(int[] boxes, int L, int R, int K, int[][][] dp) { public static int process2(int[] boxes, int L, int R, int K, int[][][] dp) {
if (L > R) { if (L > R) {
return K * K; return 0;
} }
if (dp[L][R][K] > 0) { if (dp[L][R][K] > 0) {
return dp[L][R][K]; return dp[L][R][K];
} }
int ans = 0;
int last = L; int last = L;
while (last + 1 <= R && boxes[last + 1] == boxes[L]) { while (last + 1 <= R && boxes[last + 1] == boxes[L]) {
last++; last++;
} }
int pre = K + last - L; int pre = K + last - L;
ans = (pre + 1) * (pre + 1) + process2(boxes, last + 1, R, 0, dp); int ans = (pre + 1) * (pre + 1) + process2(boxes, last + 1, R, 0, dp);
for (int i = last + 2; i <= R; i++) { for (int i = last + 2; i <= R; i++) {
if (boxes[i] == boxes[L] && boxes[i - 1] != boxes[L]) { if (boxes[i] == boxes[L] && boxes[i - 1] != boxes[L]) {
ans = Math.max(ans, process2(boxes, last + 1, i - 1, 0, dp) + process2(boxes, i, R, pre + 1, dp)); ans = Math.max(ans, process2(boxes, last + 1, i - 1, 0, dp) + process2(boxes, i, R, pre + 1, dp));

Loading…
Cancel
Save