312. Burst Balloonshtml
https://www.cnblogs.com/grandyang/p/5006441.htmlspa
dp[i][j]表示打爆區間 [i,j] 中的全部氣球能獲得的最多金幣。code
計算的順序是按照區間長度遞增的順序進行計算,從i-j長度爲1到i-j長度爲n。htm
class Solution { public: int maxCoins(vector<int>& nums) { int n = nums.size(); nums.insert(nums.begin(),1); nums.push_back(1); vector<vector<int>> dp(n+2,vector<int>(n+2,0)); for(int len = 1;len <= n;len++){ for(int i = 1;i <= n - len + 1;i++){ int j = i + len - 1; for(int k = i;k <= j;k++){ dp[i][j] = max(dp[i][j],nums[i-1]*nums[k]*nums[j+1] + dp[i][k-1] + dp[k+1][j]); } } } return dp[1][n]; } };