-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBurst_Balloons.cpp
More file actions
34 lines (25 loc) · 850 Bytes
/
Burst_Balloons.cpp
File metadata and controls
34 lines (25 loc) · 850 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
// https://leetcode.com/problems/burst-balloons/
// https://www.interviewbit.com/problems/burst-balloons/
// ** Solution approach : https://journeywithdp.blogspot.com/2019/07/burst-balloons-google-interview-question.html
int dp[105][105];
int collectCoins(int L, int R, vector <int> &val)
{
if(L > R)
return 0;
if(dp[L][R] != -1)
return dp[L][R];
int ans = 0;
for(int k = L; k <= R; k++)
ans = max(ans, collectCoins(L, k-1, val) + collectCoins(k+1, R, val) + val[k] * (L == 0 ? 1 : val[L-1]) * (R == val.size()-1 ? 1 : val[R+1]));
return dp[L][R] = ans;
}
int Solution::solve(vector<int> &nums)
{
int n = nums.size();
for(int i = 0; i < 105; i++)
{
for(int j = 0; j < 105; j++)
dp[i][j] = -1;
}
return collectCoins(0, n-1, nums);
}