Skip to content

Commit 2c4acb7

Browse files
refactor 312
1 parent 70dda00 commit 2c4acb7

File tree

1 file changed

+25
-19
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+25
-19
lines changed
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
4+
* 312. Burst Balloons
5+
*
6+
* Given n balloons, indexed from 0 to n-1.
7+
* Each balloon is painted with a number on it represented by array nums.
8+
* You are asked to burst all the balloons.
9+
* If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins.
10+
* Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
511
612
Find the maximum coins you can collect by bursting the balloons wisely.
713
@@ -20,28 +26,28 @@
2026
*/
2127
public class _312 {
2228

23-
public int maxCoins(int[] iNums) {
24-
int[] nums = new int[iNums.length + 2];
25-
int n = 1;
26-
for (int x : iNums) {
27-
if (x > 0) {
28-
nums[n++] = x;
29+
public static class Solution1 {
30+
public int maxCoins(int[] iNums) {
31+
int[] nums = new int[iNums.length + 2];
32+
int n = 1;
33+
for (int x : iNums) {
34+
if (x > 0) {
35+
nums[n++] = x;
36+
}
2937
}
30-
}
31-
nums[0] = nums[n++] = 1;
32-
33-
34-
int[][] dp = new int[n][n];
35-
for (int k = 2; k < n; ++k) {
36-
for (int left = 0; left < n - k; ++left) {
37-
int right = left + k;
38-
for (int i = left + 1; i < right; ++i) {
39-
dp[left][right] = Math.max(dp[left][right],
38+
nums[0] = nums[n++] = 1;
39+
40+
int[][] dp = new int[n][n];
41+
for (int k = 2; k < n; ++k) {
42+
for (int left = 0; left < n - k; ++left) {
43+
int right = left + k;
44+
for (int i = left + 1; i < right; ++i) {
45+
dp[left][right] = Math.max(dp[left][right],
4046
nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right]);
47+
}
4148
}
4249
}
50+
return dp[0][n - 1];
4351
}
44-
return dp[0][n - 1];
4552
}
46-
4753
}

0 commit comments

Comments
 (0)