|
4 | 4 | * Date 03/02/2016 |
5 | 5 | * @author Tushar Roy |
6 | 6 | * |
| 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 | + * |
7 | 13 | * Time complexity O(n^3) |
8 | 14 | * Space complexity O(n^2) |
| 15 | + * |
| 16 | + * Reference |
9 | 17 | * https://leetcode.com/problems/burst-balloons/ |
10 | 18 | */ |
11 | 19 | public class BurstBalloons { |
12 | | - public int maxCoins(int[] nums) { |
| 20 | + |
| 21 | + /** |
| 22 | + * Dynamic programming solution. |
| 23 | + */ |
| 24 | + public int maxCoinsBottomUpDp(int[] nums) { |
13 | 25 |
|
14 | 26 | int T[][] = new int[nums.length][nums.length]; |
15 | 27 |
|
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; |
19 | 31 | 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] |
22 | 47 | int before = 0; |
| 48 | + int after = 0; |
23 | 49 | if (i != k) { |
24 | 50 | before = T[i][k-1]; |
25 | 51 | } |
26 | | - int after = 0; |
27 | 52 | if (j != k) { |
28 | 53 | after = T[k+1][j]; |
29 | 54 | } |
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]); |
37 | 57 | } |
38 | 58 | } |
39 | 59 | } |
40 | 60 | return T[0][nums.length - 1]; |
41 | 61 | } |
42 | 62 |
|
| 63 | + /** |
| 64 | + * Recursive solution. |
| 65 | + */ |
43 | 66 | public int maxCoinsRec(int nums[]) { |
44 | 67 | int[] nums1 = new int[nums.length + 2]; |
45 | 68 | nums1[0] = 1; |
@@ -81,7 +104,7 @@ private int[] formNewArray(int[] input, int doNotIncludeIndex) { |
81 | 104 |
|
82 | 105 | public static void main(String args[]) { |
83 | 106 | 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)); |
86 | 109 | } |
87 | 110 | } |
0 commit comments