Skip to content

Commit 02daee4

Browse files
author
Tushar Roy
committed
Burst balloon DP
1 parent 55a93ec commit 02daee4

1 file changed

Lines changed: 39 additions & 16 deletions

File tree

‎src/com/interview/dynamic/BurstBalloons.java‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,65 @@
44
* Date 03/02/2016
55
* @author Tushar Roy
66
*
7+
* Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented
8+
* by array nums. You are asked to burst all the balloons. If the you burst balloon i you will
9+
* get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst,
10+
* the left and right then becomes adjacent.
11+
* Find the maximum coins you can collect by bursting the balloons wisely.
12+
*
713
* Time complexity O(n^3)
814
* Space complexity O(n^2)
15+
*
16+
* Reference
917
* https://leetcode.com/problems/burst-balloons/
1018
*/
1119
public class BurstBalloons {
12-
public int maxCoins(int[] nums) {
20+
21+
/**
22+
* Dynamic programming solution.
23+
*/
24+
public int maxCoinsBottomUpDp(int[] nums) {
1325

1426
int T[][] = new int[nums.length][nums.length];
1527

16-
for (int l = 1; l <= nums.length; l++) {
17-
for (int i = 0; i <= nums.length - l; i++) {
18-
int j = i + l - 1;
28+
for (int len = 1; len <= nums.length; len++) {
29+
for (int i = 0; i <= nums.length - len; i++) {
30+
int j = i + len - 1;
1931
for (int k = i; k <= j; k++) {
20-
int left = 1;
21-
int right = 1;
32+
//leftValue/rightValue is initially 1. If there is element on
33+
// left/right of k then left/right value will take that value.
34+
int leftValue = 1;
35+
int rightValue = 1;
36+
if (i != 0) {
37+
leftValue = nums[i-1];
38+
}
39+
if (j != nums.length -1) {
40+
rightValue = nums[j+1];
41+
}
42+
43+
//before is initially 0. If k is i then before will
44+
//stay 0 otherwise it gets value T[i][k-1]
45+
//after is similarly 0 initially. if k is j then after will
46+
//stay 0 other will get value T[k+1][j]
2247
int before = 0;
48+
int after = 0;
2349
if (i != k) {
2450
before = T[i][k-1];
2551
}
26-
int after = 0;
2752
if (j != k) {
2853
after = T[k+1][j];
2954
}
30-
if (i != 0) {
31-
left = nums[i-1];
32-
}
33-
if (j != nums.length -1) {
34-
right = nums[j+1];
35-
}
36-
T[i][j] = Math.max(left*nums[k]*right + before + after, T[i][j]);
55+
T[i][j] = Math.max(leftValue * nums[k] * rightValue + before + after,
56+
T[i][j]);
3757
}
3858
}
3959
}
4060
return T[0][nums.length - 1];
4161
}
4262

63+
/**
64+
* Recursive solution.
65+
*/
4366
public int maxCoinsRec(int nums[]) {
4467
int[] nums1 = new int[nums.length + 2];
4568
nums1[0] = 1;
@@ -81,7 +104,7 @@ private int[] formNewArray(int[] input, int doNotIncludeIndex) {
81104

82105
public static void main(String args[]) {
83106
BurstBalloons bb = new BurstBalloons();
84-
int input[] = {3, 1, 5, 8};
85-
System.out.print(bb.maxCoins(input));
107+
int input[] = {2, 4, 3, 5};
108+
System.out.print(bb.maxCoinsBottomUpDp(input));
86109
}
87110
}

0 commit comments

Comments
 (0)