|
| 1 | +package dynamic_programming; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by gouthamvidyapradhan on 02/01/2018. |
| 7 | + * Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. |
| 8 | + * You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * |
| 9 | + * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then |
| 10 | + * becomes adjacent. |
| 11 | +
|
| 12 | + Find the maximum coins you can collect by bursting the balloons wisely. |
| 13 | +
|
| 14 | + Note: |
| 15 | + (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. |
| 16 | + (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100 |
| 17 | +
|
| 18 | + Example: |
| 19 | +
|
| 20 | + Given [3, 1, 5, 8] |
| 21 | +
|
| 22 | + Return 167 |
| 23 | +
|
| 24 | + nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] |
| 25 | + coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167 |
| 26 | +
|
| 27 | + Solution: O(N ^ 3) |
| 28 | + The recursive top-down dp memorization solution is based on the idea where each balloon is considered as the last |
| 29 | + balloon to be burst. |
| 30 | + For the above example 1,3,1,5,8,1 (1 included at either end to indicate boundary) |
| 31 | + each balloon starting from 3 until 8 is chosen each time as a the last balloon to be burst using the boundary 1 on |
| 32 | + either side. |
| 33 | + So, for the first iteration the result is calculated as |
| 34 | + 3*1(left boundary)*1(right boundary) + dp(1, 3) (left-sub-problem having 1 and 3 as the boundary) + dp(3, 1) |
| 35 | + (right-sub-problem having 3 and 1 as the boundary) |
| 36 | +
|
| 37 | + */ |
| 38 | +public class BurstBalloons { |
| 39 | + private int[][] dp; |
| 40 | + private int[] N; |
| 41 | + |
| 42 | + /** |
| 43 | + * Main method |
| 44 | + * @param args |
| 45 | + * @throws Exception |
| 46 | + */ |
| 47 | + public static void main(String[] args) throws Exception{ |
| 48 | + int[] A = {3, 1, 5, 8}; |
| 49 | + System.out.println(new BurstBalloons().maxCoins(A)); |
| 50 | + } |
| 51 | + |
| 52 | + public int maxCoins(int[] nums) { |
| 53 | + N = new int[nums.length + 2]; |
| 54 | + N[0] = N[N.length - 1] = 1; //boundary |
| 55 | + for(int i = 0; i < nums.length; i ++){ |
| 56 | + N[i + 1] = nums[i]; |
| 57 | + } |
| 58 | + dp = new int[N.length][N.length]; |
| 59 | + for (int[] aDp : dp) { |
| 60 | + Arrays.fill(aDp, -1); |
| 61 | + } |
| 62 | + return dp(0, N.length - 1); |
| 63 | + } |
| 64 | + |
| 65 | + private int dp(int l, int r){ |
| 66 | + if(l + 1 == r) return 0; |
| 67 | + if(dp[l][r] != -1) return dp[l][r]; |
| 68 | + int result = 0; |
| 69 | + for(int i = l + 1; i < r; i ++){ |
| 70 | + result = Math.max(result, N[i] * N[l] * N[r] + dp(l, i) + dp(i, r)); |
| 71 | + } |
| 72 | + dp[l][r] = result; |
| 73 | + return dp[l][r]; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +} |
0 commit comments