- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Burst Balloons in C++
Suppose we have n balloons, these are indexed from 0 to n-1. Here each balloon is painted with a number on it represented by one array called nums. we have to burst all the balloons. If we burst balloon i we will get nums[i – 1] * nums[i] * nums[i + 1] number of coins. After the burst, the i – 1 and i + 1 then becomes adjacent. We have to find the maximum coins to collect by bursting the balloons wisely.
So if the input is like [3,1,5,7], then the result will be 148. Initially the array is like [3,1,5,7], then after bursting 1, we will get 3 * 1 * 5 = 15, then the array is [3,5,7], then burst 5, so we will get (3 * 5 * 7) = 105, then array is like [3,7], then burst 3, so we will get (1*3*7) = 21, finally the array is [7], so after bursting, we will get 7, so the total is 15 + 105 + 21 + 7 = 148.
To solve this, we will follow these steps −
n := size of a
if (n is non-zero) is false, then,
return 0
Define one 2D array dp of order n x n
for initializing l := n - 1, when l >= 0, decrease l by 1 do −
for initializing r := l, when r < n, increase r by 1 do −
for initializing i := l, when i <= r, increase i by 1 do −
y := dp[i + 1, r] if i + 1 < n, otherwise 0
z := a[l - 1] if l - 1 >= 0 otherwise 1
w := a[r + 1] if r + 1 < n otherwise 1
x := dp[l, i - 1] if i - 1 > = 0, otherwise 0 + y + (z * w * a[i])
dp[l, r] := max of dp[l, r] and x
return dp[0, n - 1]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int maxCoins(vector<int>& a) { int n = a.size(); if(!n)return 0; vector < vector <int>> dp(n,vector <int> (n)); for(int l = n-1;l>=0;l--){ for(int r=l;r<n;r++){ for(int i =l;i<=r;i++){ dp[l][r] = max(dp[l][r],(i-1>=0?dp[l][i-1]:0) +(i+1<n?dp[i+1][r]:0)+((l-1>=0?a[l-1]:1 )*(r+1<n?a[r+1]:1)*a[i])); } } } return dp[0][n-1]; } }; main(){ Solution ob; vector<int> v = {3,1,5,7}; cout << (ob.maxCoins(v)); }
Input
[3,1,5,7]
Output
148
- Related Articles
- Minimum Number of Arrows to Burst Balloons in C++
- Why the hot air balloons do not come back to the earth and the hot air balloons also have mass?
- Boojho saw a cracker burst at night at a distance from his house. He heard the sound of the cracker a little later after seeing the cracker burst. Give reason for the delay in hearing the sound.
- If I rub two balloons with the woollen clothes by different directions do they have the same charges in it?
- It is much easier to burst an inflated balloon with a needle than by a finger. Explain.
- Why do the bodies of deep-sea fishes burst open while brought to the surface of the sea?
- Adil parked his bicycle on a sunny day in a parking stand of his school campus. When the school got over Adil saw his burst cycle type. Thereafter he kept less air in his cycle types and did not inflate them fully.(a) Why did the type burst?(b) Why is air compressible?(c) What value of Adil is reflected in the above act?
- lt was Paheli’s birthday, her brother Simba was helping her to decorate the house for the birthday party and their parents were also busy making other arrangements. Following were the activities going on at Paheli’s home:(i) Simba blew balloons and put them on the wall.(ii) Some of the balloons got burst.(iii) Paheli cut colourful strips of paper and put them on the wall with the help of tape.(iv) She also made some flowers by origami (paper folding) to decorate the house.(v) Her father made dough balls.(vi) Mother rolled the dough balls to make puries.(vii) Mother heated oil in a pan.(viii) Father fried the puries in hot oil.Identify the activities at Paheli’s home as those that can be reversed and those which cannot be reversed.
- Project:Requirements:2 test tubes, marker pen, sugar, yeast powder, 2 balloons and lime water.Method:Take two test tubes and mark them A and B. Clamp these tubes in a stand and fill them with water, leaving some space at the top. Put two spoonfuls of sugar in each of the test tubes. Add a spoonful of yeast to test tube B. Inflate the two balloons incompletely. Now tie the balloons to the mouths of each test tube. Keep them in a warm place, away from sunlight. Watch the setup every day for the next 3-4 days. Record your observations and think of an explanation.Now take another test tube filled 1/4 with lukewarm water. Remove the balloon from test tube B in such a manner that the gas inside the balloon does not escape. Fit the balloon into the test tube and shake well. Observe and explain."
- A child watching Dussehra from a distance sees the effigy of Ravana burst into flames and hears the explosion associated with it $2 s$ after that. How far was he from the effigy if the speed of sound in air that night was $335 m/sec$?
- fseek() in C/C++
- strcpy() in C/C++
- strcmp() in C/C++
- strcoll() in C/C++
- isless() in C/C++
