-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBurst Balloons.cpp
More file actions
34 lines (25 loc) · 868 Bytes
/
Burst Balloons.cpp
File metadata and controls
34 lines (25 loc) · 868 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
25
26
27
28
29
30
31
32
33
34
/*
Problem Link: https://practice.geeksforgeeks.org/problems/burst-balloons/1
*/
// -----Approach 1: Tabulation ------------------------------------------------------------
class Solution {
public:
int maxCoins(int N, vector<int> &arr) {
int n= arr.size();
vector<vector<int>> dp(n+2, vector<int>(n+2, 0)); // n+2 due to idx+1
arr.insert(arr.begin(), 1);
arr.push_back(1);
for(int i=n; i>=1; i--){
for(int j=1; j<=n; j++){
if(i > j) continue;
int mx= -1e9;
for(int idx=i; idx<=j; idx++){
int cost= arr[i-1] * arr[idx] * arr[j+1] + dp[i][idx-1] + dp[idx+1][j];
mx= max(mx, cost);
}
dp[i][j]= mx;
}
}
return dp[1][n];
}
};