forked from JFulgoni/Java-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBurstBalloons.java
More file actions
45 lines (40 loc) · 1.05 KB
/
Copy pathBurstBalloons.java
File metadata and controls
45 lines (40 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package john_test;
public class BurstBalloons {
public BurstBalloons(){
}
// https://leetcode.com/discuss/82998/jave-understand-illustrated-picture-take-look-still-confused
public int play(int[] balloons){
if(balloons == null || balloons.length == 0){
return 0;
}
int n = balloons.length;
if(n == 1){
return balloons[0];
}
int[][]coins = new int[n + 1][n + 1]; //coins[i][j] is the value by bursting coins in the [i, j) range
for(int j = 1; j <= n; j++){
for(int i = j - 1; i >= 0; --i){
int max = 0;
for(int k = i; k <= j - 1; k++){ //k is the index of the last balloon bursted in [i, j)
int right, left;
if(j == n){
right = 1;
}
else{
right = balloons[j];
}
if(i == 0){
left = 1;
}
else{
left = balloons[i - 1];
}
max = Math.max(max, coins[i][k] + coins[k + 1][j] + balloons[k] * right * left);
//[i+1][j] is computed before [i][j] which makes this formula possible
}//end k
coins[i][j] = max;
}//end i
}//end j
return coins[0][n];
}
}