312. Burst Balloons

Question

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.

Find the maximum coins you can collect by bursting the balloons wisely.

Note:

  • You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
  • 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
Example:

Input: [3,1,5,8]
Output: 167 
Explanation: nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
             coins =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167

Thinking:

  • Method 1:backtrace TLE
    class Solution {
      private int max = 0;
      public int maxCoins(int[] nums) {
          int result = 0;
          if(nums == null || nums.length == 0) return 0;
          backtrace(nums, new boolean[nums.length], 0, 0);
          return this.max;
      }
      private void backtrace(int[] nums, boolean[] visited, int count, int sum){
          if(count == nums.length){
              this.max = Math.max(sum, max);
              return;
          }
          for(int i = 0; i < nums.length; i++){
              int temp = 0;
              if(visited[i]) continue;
              visited[i] = true;
              int left = 1;
              for(int j = i - 1; j >= 0; j--){
                  if(!visited[j]){
                      left = nums[j]; break;
                  }
              }
              int right = 1;
              for(int j = i + 1; j < nums.length; j++){
                  if(!visited[j]){
                      right = nums[j]; break;
                  }
              }
              backtrace(nums, visited, count + 1, sum + left * nums[i] * right);
              visited[i] = false;
          }
      }
    }
    
  • Method 2: DP, this question is a little bit difficult and worth having more conclusion.
    1. We create a new array with nums[len + 2], first and last index are filled with 1.
    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].
    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]).
      • dp[i][mid - 1]: the sub result of left side of last burst ballon(mid)
      • arr[i - 1] * arr[mid] * arr[j + 1]: last one * remained left * remained right.
      • dp[mid + 1][j]: the sub result of right side of last burst ballon(mid)
    4. How to traversal the array:
      • dist: the distance from left index.
      • left: the left position.[1, len - dist + 1]
      • right: [left, left + dist - 1]
      • mid: last burst ballon. [left, right]
        class Solution {
          public int maxCoins(int[] nums) {
         if(nums == null || nums.length == 0) return 0;
         int len = nums.length;
         int[][] dp = new int[len + 2][len + 2];
         int[] arr = new int[len + 2];
         for(int i = 1; i < len + 1; i++){
          arr[i] = nums[i - 1];
          dp[i][i] = arr[i];
         }
         arr[0] = arr[len + 1] = 1;
         for(int dist = 1; dist <= len; dist++){
          for(int left = 1; left <= len - dist + 1; left++){
              int right = left + dist - 1;
              for(int mid = left; mid <= right; mid ++){
                  dp[left][right] = Math.max(dp[left][right], dp[left][mid - 1] + arr[left - 1] * arr[mid] * arr[right + 1] + dp[mid + 1 ][right]);
              }
          }
         }
         return dp[1][len];
          }
        }
        

Reference

  1. 312. Burst Balloons