Skip to content

Commit 5e83dd3

Browse files
authored
Merge pull request #10 from biswajit-k/patch-1
Update burst balloon 1.cpp to consider balloons with 0 points
2 parents 909cf48 + 54b5bbe commit 5e83dd3

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

burst balloon 1.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ Whenever a particular balloon is shot the no of points increases by
88
99
You have to output the maximum no of points possible.
1010
11-
Input
11+
Input-1
1212
1 2 3 4
1313
14-
Output
14+
Output-1
1515
20
16+
17+
Input-2
18+
1 0 2 3 0 4
19+
20+
Output-2
21+
34
1622
*/
1723

1824
#include <iostream>
@@ -21,7 +27,7 @@ using namespace std;
2127
int maxcoins(int A[],int siz)
2228
{
2329
int nums[siz+2];
24-
int n=1;
30+
int n=1, points = 0;
2531

2632
for(int i=0;i<siz;i++)
2733
{
@@ -30,10 +36,14 @@ int maxcoins(int A[],int siz)
3036
nums[n] = A[i];
3137
n++;
3238
}
39+
else
40+
points += (n - 1 > 0 ? nums[n - 1] : 1) * (i + 1 < siz ? A[i + 1] : 1); // balloons with zero points should be removed first using previous and next balloon points
41+
3342
}
3443
nums[0] = nums[n] = 1;
3544
n++;
36-
45+
46+
// dp[i][j]: maximum points that can be collected from balloons from index i to j, (i and j exclusive)
3747
int dp[n][n] = {};
3848

3949
for(int j=2;j<n;j++)
@@ -58,7 +68,7 @@ int maxcoins(int A[],int siz)
5868
cout<<endl;
5969
}
6070
}
61-
return dp[0][n-1];
71+
return points + dp[0][n-1];
6272
}
6373

6474
int main()
@@ -77,4 +87,4 @@ int main()
7787
/*
7888
5
7989
1 2 3 5 4
80-
*/
90+
*/

0 commit comments

Comments
 (0)