-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_312_BurstBalloons.cpp
47 lines (35 loc) · 1.1 KB
/
_312_BurstBalloons.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Source - https://leetcode.com/problems/burst-balloons/
Author - Shivam Arora
*/
#include <bits/stdc++.h>
using namespace std;
int maxCoins(vector<int>& nums) {
if(nums.size() == 0)
return 0;
int n = nums.size(), dp[n][n];
memset(dp, 0, sizeof(dp));
for(int k = 0; k < n; k++) {
for(int i = 0, j = k; j < n; i++, j++) {
int lval = (i == 0) ? 1 : nums[i - 1];
int rval = (j == n - 1) ? 1 : nums[j + 1];
dp[i][j] = INT_MIN;
for(int cut = i; cut <= j; cut++) {
int left = (cut == i) ? 0 : dp[i][cut - 1];
int right = (cut == j) ? 0 : dp[cut + 1][j];
dp[i][j] = max(dp[i][j], left + (lval * nums[cut] * rval) + right);
}
}
}
return dp[0][n - 1];
}
int main()
{
int n;
cout<<"Enter number of balloons: ";
cin>>n;
vector<int> nums(n);
cout<<"Enter numbers on the balloons: ";
for(int i = 0; i < n; i++)
cin>>nums[i];
cout<<"Maximum coins collected by bursting all the balloons: "<<maxCoins(nums)<<endl;
}