|
| 1 | +## 312. Burst Balloons |
| 2 | + |
| 3 | +### Question |
| 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. |
| 5 | + |
| 6 | +Find the maximum coins you can collect by bursting the balloons wisely. |
| 7 | + |
| 8 | +Note: |
| 9 | +* You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. |
| 10 | +* 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100 |
| 11 | + |
| 12 | +``` |
| 13 | +Example: |
| 14 | +
|
| 15 | +Input: [3,1,5,8] |
| 16 | +Output: 167 |
| 17 | +Explanation: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] |
| 18 | + coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167 |
| 19 | +``` |
| 20 | + |
| 21 | +### Thinking: |
| 22 | +* Method 1:backtrace TLE |
| 23 | +```Java |
| 24 | +class Solution { |
| 25 | + private int max = 0; |
| 26 | + public int maxCoins(int[] nums) { |
| 27 | + int result = 0; |
| 28 | + if(nums == null || nums.length == 0) return 0; |
| 29 | + backtrace(nums, new boolean[nums.length], 0, 0); |
| 30 | + return this.max; |
| 31 | + } |
| 32 | + private void backtrace(int[] nums, boolean[] visited, int count, int sum){ |
| 33 | + if(count == nums.length){ |
| 34 | + this.max = Math.max(sum, max); |
| 35 | + return; |
| 36 | + } |
| 37 | + for(int i = 0; i < nums.length; i++){ |
| 38 | + int temp = 0; |
| 39 | + if(visited[i]) continue; |
| 40 | + visited[i] = true; |
| 41 | + int left = 1; |
| 42 | + for(int j = i - 1; j >= 0; j--){ |
| 43 | + if(!visited[j]){ |
| 44 | + left = nums[j]; break; |
| 45 | + } |
| 46 | + } |
| 47 | + int right = 1; |
| 48 | + for(int j = i + 1; j < nums.length; j++){ |
| 49 | + if(!visited[j]){ |
| 50 | + right = nums[j]; break; |
| 51 | + } |
| 52 | + } |
| 53 | + backtrace(nums, visited, count + 1, sum + left * nums[i] * right); |
| 54 | + visited[i] = false; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +* Method 2: DP, this question is a little bit difficult and worth having more conclusion. |
| 61 | + 1. We create a new array with nums[len + 2], first and last index are filled with 1. |
| 62 | + 2. we create a dp[i][j], i means the starting position and j means end position, both included. The final result is dp[1][len]. |
| 63 | + 3. we define the last burst balloon index as mid, we have dp[i][j] = max(dp[i][j], dp[i][mid - 1] + arr[i - 1] * arr[mid] * arr[j + 1] + dp[mid + 1][j]). |
| 64 | + * dp[i][mid - 1]: the sub result of left side of last burst ballon(mid) |
| 65 | + * arr[i - 1] * arr[mid] * arr[j + 1]: last one * remained left * remained right. |
| 66 | + * dp[mid + 1][j]: the sub result of right side of last burst ballon(mid) |
| 67 | + 3. How to traversal the array: |
| 68 | + * dist: the distance from left index. |
| 69 | + * left: the left position.[1, len - dist + 1] |
| 70 | + * right: [left, left + dist - 1] |
| 71 | + * mid: last burst ballon. [left, right] |
| 72 | +```Java |
| 73 | +class Solution { |
| 74 | + public int maxCoins(int[] nums) { |
| 75 | + if(nums == null || nums.length == 0) return 0; |
| 76 | + int len = nums.length; |
| 77 | + int[][] dp = new int[len + 2][len + 2]; |
| 78 | + int[] arr = new int[len + 2]; |
| 79 | + for(int i = 1; i < len + 1; i++){ |
| 80 | + arr[i] = nums[i - 1]; |
| 81 | + dp[i][i] = arr[i]; |
| 82 | + } |
| 83 | + arr[0] = arr[len + 1] = 1; |
| 84 | + for(int dist = 1; dist <= len; dist++){ |
| 85 | + for(int left = 1; left <= len - dist + 1; left++){ |
| 86 | + int right = left + dist - 1; |
| 87 | + for(int mid = left; mid <= right; mid ++){ |
| 88 | + dp[left][right] = Math.max(dp[left][right], dp[left][mid - 1] + arr[left - 1] * arr[mid] * arr[right + 1] + dp[mid + 1 ][right]); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return dp[1][len]; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Reference |
| 98 | +1. [312. Burst Balloons](https://blog.csdn.net/zjucor/article/details/56481930) |
| 99 | + |
| 100 | + |
0 commit comments