# Hard-题目3：312. Burst Balloons

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

dp[i][j]=min⁡(dp[i][k-1]+num[k-1]*num[k]*num[k+1]+dp[k+1][j]),k∈(i,j)

class Solution {
public:
int maxCoins(vector<int>& nums) {
for(int i=0;i<nums.size();++i){
if(nums[i]==0){
nums.erase(nums.begin()+i);
--i;
}
}
int n=nums.size();
if(n==0) return 0;
nums.insert(nums.begin(),1);
nums.insert(nums.end(),1);
int m=nums.size();
vector<vector<int>> dp(m,vector<int>(m,0));
for(int count=1;count<=n;++count){
for(int start=1;start+count-1<=n;++start){
int bestcoins=0;
for(int b=0;b<count;++b){
bestcoins=max(bestcoins,dp[start][start+b-1]+nums[start-1]*nums[start+b]*nums[start+count]+dp[start+b+1][start+count-1]);

}
dp[start][start+count-1]=bestcoins;
}

}
return dp[1][n];
}
};

36ms,beats 61.75%,众数44ms,22.51%
Cmershen的碎碎念：

#### leetcode 312. Burst Balloons 分析

2016-01-22 14:39:04

#### LeetCode 312. Burst Balloons

2016-04-05 00:03:23

#### [leetcode] 312. Burst Balloons 解题报告

2016-05-13 15:35:07

#### LeetCode 312. Burst Balloons（戳气球）

2016-04-21 14:39:10

#### 312 Burst Balloons

2015-12-01 15:41:38

#### [LeetCode]Burst Balloons 爆气球

2017-03-19 17:33:59

#### Leetcode 452. Minimum Number of Arrows to Burst Balloons 射箭 解题报告

2016-11-09 10:15:43

#### leetcode Burst Balloons

2015-11-30 14:52:41

#### LeetCode 题目：312. Burst Balloons

2017-03-18 20:59:27

#### 【leetcode题解】【再来，动态规划】【H】【21.8】Burst Balloons

2015-12-21 19:19:52