Skip to content

Commit 7b23b18

Browse files
committed
UPDATE: Burst Balloons in required format
1 parent da13827 commit 7b23b18

File tree

1 file changed

+106
-32
lines changed

1 file changed

+106
-32
lines changed
Lines changed: 106 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,119 @@
1-
#include<iostream>
2-
#include<vector>
1+
/**
2+
* @file
3+
* @brief Implementation of Burst Balloons problem
4+
*
5+
* @details
6+
* You are given `n` balloons, each with a number on it represented by an array
7+
* `nums`. You are asked to burst all the balloons. If you burst the `i-th`
8+
* balloon, you will get `nums[i - 1] * nums[i] * nums[i + 1]` coins. The goal
9+
* is to maximize the coins collected by bursting the balloons wisely.
10+
*
11+
* ### Algorithm
12+
* We use dynamic programming with memoization. For each subproblem defined by
13+
* bursting a balloon at index `k`, we calculate the maximum coins from two
14+
* smaller subproblems and store the results for future use to avoid recalculations.
15+
*
16+
* @author [Saptarshi Ghosh](https://github.com/codec404)
17+
*/
18+
19+
#include <iostream>
20+
#include <vector>
21+
#include <cassert>
322

423
using namespace std;
524

6-
// Top Down Approach
25+
/**
26+
* @namespace dynamic_programming
27+
* @brief Dynamic Programming algorithms
28+
*/
29+
namespace dynamic_programming {
30+
31+
/**
32+
* @namespace burst_balloons
33+
* @brief Implementation of the Burst Balloons problem
34+
*/
35+
namespace burst_balloons {
736

8-
int recurse(int i,int j,vector<int> &nums,vector<vector<int>> &dp){
9-
if(i > j){
37+
/**
38+
* @brief Recursive function to calculate maximum coins
39+
* @param i Start index of the range of balloons
40+
* @param j End index of the range of balloons
41+
* @param nums Vector of balloon numbers
42+
* @param dp Memoization table to store intermediate results
43+
* @return Maximum coins obtainable by bursting balloons from index i to j
44+
*/
45+
int recurse(int i, int j, vector<int> &nums, vector<vector<int>> &dp) {
46+
if (i > j) {
1047
return 0;
1148
}
12-
if(dp[i][j] != -1) return dp[i][j];
13-
int maxi = 1e9;
14-
for(int k = i;k <= j;k++){
15-
int coins = (nums[i-1] * nums[k] * nums[j+1])+ recurse(i,k-1,nums,dp) + recurse(k+1,j,nums,dp);
16-
maxi = max(maxi,coins);
49+
if (dp[i][j] != -1) return dp[i][j];
50+
51+
int maxi = 0;
52+
for (int k = i; k <= j; k++) {
53+
int coins = (nums[i - 1] * nums[k] * nums[j + 1])
54+
+ recurse(i, k - 1, nums, dp)
55+
+ recurse(k + 1, j, nums, dp);
56+
maxi = max(maxi, coins);
1757
}
1858
return dp[i][j] = maxi;
1959
}
20-
int maxCoins(vector<int> &nums){
60+
61+
/**
62+
* @brief Function to calculate the maximum coins obtainable by bursting balloons
63+
* @param nums Vector of balloon numbers
64+
* @return Maximum coins that can be collected
65+
*/
66+
int maxCoins(vector<int> &nums) {
2167
int n = nums.size();
22-
nums.insert(nums.begin(),1);
23-
nums.push_back(1);
24-
vector<vector<int>> dp(n+2,vector<int>(n+2,-1));
25-
int ans = recurse(1,n,nums,dp);
26-
return ans;
68+
nums.insert(nums.begin(), 1); // Add 1 at the beginning
69+
nums.push_back(1); // Add 1 at the end
70+
vector<vector<int>> dp(n + 2, vector<int>(n + 2, -1)); // Memoization table
71+
return recurse(1, n, nums, dp); // Solve the problem for the full array
2772
}
28-
void solve(){
29-
int n; // Taking the number of elements in the array
30-
cin >> n;
31-
vector<int> nums(n);
32-
for(auto &x: nums){
33-
cin >> x;
34-
}
35-
int ans = maxCoins(nums);
36-
cout << ans << endl;
73+
74+
} // namespace burst_balloons
75+
} // namespace dynamic_programming
76+
77+
/**
78+
* @brief Function to test the Burst Balloons algorithm
79+
* @returns void
80+
*/
81+
static void test() {
82+
// Test 1: Example from the problem statement
83+
vector<int> nums1 = {3, 1, 5, 8}; // Input array
84+
int expected_result1 = 167; // Expected output
85+
int result1 = dynamic_programming::burst_balloons::maxCoins(nums1);
86+
assert(result1 == expected_result1);
87+
cout << "Test 1 passed: Maximum coins = " << result1 << endl;
88+
89+
// Test 2: Another example
90+
vector<int> nums2 = {1, 5}; // Input array
91+
int expected_result2 = 10; // Expected output
92+
int result2 = dynamic_programming::burst_balloons::maxCoins(nums2);
93+
assert(result2 == expected_result2);
94+
cout << "Test 2 passed: Maximum coins = " << result2 << endl;
95+
96+
// Test 3: Single balloon
97+
vector<int> nums3 = {9}; // Input array
98+
int expected_result3 = 9; // Expected output
99+
int result3 = dynamic_programming::burst_balloons::maxCoins(nums3);
100+
assert(result3 == expected_result3);
101+
cout << "Test 3 passed: Maximum coins = " << result3 << endl;
102+
103+
// Test 4: Larger input
104+
vector<int> nums4 = {100,23,15,10,24,12,22,10}; // Input array
105+
int expected_result4 = 156336; // Expected output
106+
int result4 = dynamic_programming::burst_balloons::maxCoins(nums4);
107+
assert(result4 == expected_result4);
108+
cout << "Test 4 passed: Maximum coins = " << result4 << endl;
37109
}
38-
int main(){
39-
int testcases = 1; // Setting the number of testcases
40-
// cin >> testcases;
41-
while(testcases--){
42-
solve();
43-
}
110+
111+
/**
112+
* @brief Main function to run tests
113+
* @returns 0 on exit
114+
*/
115+
int main() {
116+
// Run tests
117+
test();
44118
return 0;
45-
}
119+
}

0 commit comments

Comments
 (0)