Skip to content

Commit 4c13031

Browse files
authored
Create lc312-dp-hard.md
1 parent 27353b6 commit 4c13031

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

leetcode/lc312-dp-hard.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
https://leetcode.com/problems/burst-balloons/
2+
3+
# 题解
4+
5+
一列连续的气球, 每个的价值为 nums[i],烧掉其中一个获得收益 nums[i-1] * nums[i] * nums[i+1], 求烧完所有气球的最大收益
6+
7+
区间dp,dp[l][r] 表示烧完(l,r)的收益,需要注意的时这个问题要反过来想,最后烧完气球i,的最大收益,这样才不会影响[l,i-1],[i+1,r]
8+
9+
```cpp
10+
class Solution {
11+
public:
12+
int dfs(int l,int r,const vector<int>& nums, vector<vector<int>>& dp){
13+
if(r<l)
14+
return 0;
15+
if(dp[l][r] != -1)
16+
return dp[l][r];
17+
int & ans = dp[l][r];
18+
for(int i=l; i<=r ; ++i){
19+
ans = max(ans, dfs(l,i-1,nums, dp) + dfs(i+1,r,nums,dp) + nums[i]*(l-1<0?1:nums[l-1]) * (r+1<nums.size()?nums[r+1]:1));
20+
}
21+
return ans;
22+
}
23+
int maxCoins(vector<int>& nums) {
24+
int n= nums.size();
25+
vector<vector<int>>dp(n, vector<int>(n, -1));
26+
return dfs(0, n-1, nums, dp);
27+
}
28+
};
29+
30+
```

0 commit comments

Comments
 (0)