package com.fishercoder.solutions;

/**
 * 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:
 (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
 (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

 Example:

 Given [3, 1, 5, 8]

 Return 167

 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
 */
public class _312 {

    public int maxCoins(int[] iNums) {
        int[] nums = new int[iNums.length + 2];
        int n = 1;
        for (int x : iNums) {
            if (x > 0) {
                nums[n++] = x;
            }
        }
        nums[0] = nums[n++] = 1;


        int[][] dp = new int[n][n];
        for (int k = 2; k < n; ++k) {
            for (int left = 0; left < n - k; ++left) {
                int right = left + k;
                for (int i = left + 1; i < right; ++i) {
                    dp[left][right] = Math.max(dp[left][right],
                            nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right]);
                }
            }
        }
        return dp[0][n - 1];
    }

}

@@ -33,16 +33,42 @@
 s;%0A%0A/**%0A
+ * 312. Burst Balloons%0A *%0A
  * Given
@@ -102,16 +102,19 @@
  to n-1.
+%0A *
  Each ba
@@ -176,16 +176,19 @@
 ay nums.
+%0A *
  You are
@@ -220,16 +220,19 @@
 alloons.
+%0A *
  If the 
@@ -305,16 +305,19 @@
 %5D coins.
+%0A *
  Here le
@@ -825,16 +825,56 @@
 _312 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -896,32 +896,36 @@
 (int%5B%5D iNums) %7B%0A
+    
         int%5B%5D nu
@@ -960,24 +960,28 @@
 2%5D;%0A        
+    
 int n = 1;%0A 
@@ -979,32 +979,36 @@
  n = 1;%0A        
+ 
+   
 for (int x : iNu
@@ -1025,16 +1025,20 @@
         
+   
+ 
 if (x %3E 
@@ -1038,24 +1038,28 @@
 f (x %3E 0) %7B%0A
+    
             
@@ -1085,34 +1085,41 @@
             
+ 
-%7D%0A
+   %7D%0A   
         
 %7D%0A        nu
@@ -1098,34 +1098,39 @@
    %7D%0A           
+ 
 %7D%0A
+    
         nums%5B0%5D 
@@ -1147,25 +1147,24 @@
 %5D = 1;%0A%0A
-%0A
         
 int%5B%5D%5B%5D 
@@ -1151,24 +1151,28 @@
 1;%0A%0A        
+    
 int%5B%5D%5B%5D dp =
@@ -1187,32 +1187,36 @@
 %5Bn%5D%5Bn%5D;%0A        
+  
+  
 for (int k = 2; 
@@ -1225,24 +1225,28 @@
 %3C n; ++k) %7B%0A
+    
             
@@ -1300,24 +1300,28 @@
             
+    
 int right = 
@@ -1330,16 +1330,20 @@
 ft + k;%0A
+    
         
@@ -1387,24 +1387,28 @@
 ght; ++i) %7B%0A
+    
             
@@ -1553,16 +1553,38 @@
 ight%5D);%0A
+                    %7D%0A
         
@@ -1607,30 +1607,24 @@
       %7D%0A    
-    %7D%0A
         retu
@@ -1644,13 +1644,22 @@
 1%5D;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 312