forked from garvit-bhardwaj/Leetcode-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBurst Balloons.cpp
More file actions
24 lines (24 loc) · 817 Bytes
/
Burst Balloons.cpp
File metadata and controls
24 lines (24 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int maxCoins(vector<int>& nums) {
//including the nums[-1] and nums[n]
int n = nums.size() + 2;
vector<vector<int>> dp(n, vector<int>(n));
vector<int> new_nums(n, 1);
int i = 1;
for(auto num : nums) {
new_nums[i++] = num;
}
for(int len = 2; len <= n; len++) {
//iterate from interval length from 2 to n
for(int i = 0; i <= n - len; i++) {
int j = i + len - 1;
//select between left and right boundary (i, j)
for(int k = i + 1; k < j; k++) {
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + new_nums[i] * new_nums[k] * new_nums[j]);
}
}
}
return dp[0][n - 1];
}
};