You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: burst balloon 1.cpp
+16-6Lines changed: 16 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -8,11 +8,17 @@ Whenever a particular balloon is shot the no of points increases by
8
8
9
9
You have to output the maximum no of points possible.
10
10
11
-
Input
11
+
Input-1
12
12
1 2 3 4
13
13
14
-
Output
14
+
Output-1
15
15
20
16
+
17
+
Input-2
18
+
1 0 2 3 0 4
19
+
20
+
Output-2
21
+
34
16
22
*/
17
23
18
24
#include<iostream>
@@ -21,7 +27,7 @@ using namespace std;
21
27
intmaxcoins(int A[],int siz)
22
28
{
23
29
int nums[siz+2];
24
-
int n=1;
30
+
int n=1, points = 0;
25
31
26
32
for(int i=0;i<siz;i++)
27
33
{
@@ -30,10 +36,14 @@ int maxcoins(int A[],int siz)
30
36
nums[n] = A[i];
31
37
n++;
32
38
}
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
+
33
42
}
34
43
nums[0] = nums[n] = 1;
35
44
n++;
36
-
45
+
46
+
// dp[i][j]: maximum points that can be collected from balloons from index i to j, (i and j exclusive)
0 commit comments