-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add burst balloons problem #2749
Open
codec404
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
codec404:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,119 @@ | ||||
/** | ||||
* @file | ||||
* @brief Implementation of Burst Balloons problem | ||||
* | ||||
* @details | ||||
* You are given `n` balloons, each with a number on it represented by an array | ||||
* `nums`. You are asked to burst all the balloons. If you burst the `i-th` | ||||
* balloon, you will get `nums[i - 1] * nums[i] * nums[i + 1]` coins. The goal | ||||
* is to maximize the coins collected by bursting the balloons wisely. | ||||
* | ||||
* ### Algorithm | ||||
* We use dynamic programming with memoization. For each subproblem defined by | ||||
* bursting a balloon at index `k`, we calculate the maximum coins from two | ||||
* smaller subproblems and store the results for future use to avoid recalculations. | ||||
* | ||||
* @author [Saptarshi Ghosh](https://github.com/codec404) | ||||
*/ | ||||
|
||||
#include <iostream> | ||||
#include <vector> | ||||
#include <cassert> | ||||
|
||||
using namespace std; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
/** | ||||
* @namespace dynamic_programming | ||||
* @brief Dynamic Programming algorithms | ||||
*/ | ||||
namespace dynamic_programming { | ||||
|
||||
/** | ||||
* @namespace burst_balloons | ||||
* @brief Implementation of the Burst Balloons problem | ||||
*/ | ||||
namespace burst_balloons { | ||||
|
||||
/** | ||||
* @brief Recursive function to calculate maximum coins | ||||
* @param i Start index of the range of balloons | ||||
* @param j End index of the range of balloons | ||||
* @param nums Vector of balloon numbers | ||||
* @param dp Memoization table to store intermediate results | ||||
* @return Maximum coins obtainable by bursting balloons from index i to j | ||||
*/ | ||||
int recurse(int i, int j, vector<int> &nums, vector<vector<int>> &dp) { | ||||
if (i > j) { | ||||
return 0; | ||||
} | ||||
if (dp[i][j] != -1) return dp[i][j]; | ||||
|
||||
int maxi = 0; | ||||
for (int k = i; k <= j; k++) { | ||||
int coins = (nums[i - 1] * nums[k] * nums[j + 1]) | ||||
+ recurse(i, k - 1, nums, dp) | ||||
+ recurse(k + 1, j, nums, dp); | ||||
maxi = max(maxi, coins); | ||||
} | ||||
return dp[i][j] = maxi; | ||||
} | ||||
|
||||
/** | ||||
* @brief Function to calculate the maximum coins obtainable by bursting balloons | ||||
* @param nums Vector of balloon numbers | ||||
* @return Maximum coins that can be collected | ||||
*/ | ||||
int maxCoins(vector<int> &nums) { | ||||
int n = nums.size(); | ||||
nums.insert(nums.begin(), 1); // Add 1 at the beginning | ||||
nums.push_back(1); // Add 1 at the end | ||||
vector<vector<int>> dp(n + 2, vector<int>(n + 2, -1)); // Memoization table | ||||
return recurse(1, n, nums, dp); // Solve the problem for the full array | ||||
} | ||||
|
||||
} // namespace burst_balloons | ||||
} // namespace dynamic_programming | ||||
|
||||
/** | ||||
* @brief Function to test the Burst Balloons algorithm | ||||
* @returns void | ||||
*/ | ||||
static void test() { | ||||
// Test 1: Example from the problem statement | ||||
vector<int> nums1 = {3, 1, 5, 8}; // Input array | ||||
int expected_result1 = 167; // Expected output | ||||
int result1 = dynamic_programming::burst_balloons::maxCoins(nums1); | ||||
assert(result1 == expected_result1); | ||||
cout << "Test 1 passed: Maximum coins = " << result1 << endl; | ||||
|
||||
// Test 2: Another example | ||||
vector<int> nums2 = {1, 5}; // Input array | ||||
int expected_result2 = 10; // Expected output | ||||
int result2 = dynamic_programming::burst_balloons::maxCoins(nums2); | ||||
assert(result2 == expected_result2); | ||||
cout << "Test 2 passed: Maximum coins = " << result2 << endl; | ||||
|
||||
// Test 3: Single balloon | ||||
vector<int> nums3 = {9}; // Input array | ||||
int expected_result3 = 9; // Expected output | ||||
int result3 = dynamic_programming::burst_balloons::maxCoins(nums3); | ||||
assert(result3 == expected_result3); | ||||
cout << "Test 3 passed: Maximum coins = " << result3 << endl; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add empty and negative test cases or use uint32_t as the parameter type |
||||
|
||||
// Test 4: Larger input | ||||
vector<int> nums4 = {100,23,15,10,24,12,22,10}; // Input array | ||||
int expected_result4 = 156336; // Expected output | ||||
int result4 = dynamic_programming::burst_balloons::maxCoins(nums4); | ||||
assert(result4 == expected_result4); | ||||
cout << "Test 4 passed: Maximum coins = " << result4 << endl; | ||||
} | ||||
|
||||
/** | ||||
* @brief Main function to run tests | ||||
* @returns 0 on exit | ||||
*/ | ||||
int main() { | ||||
// Run tests | ||||
test(); | ||||
return 0; | ||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These headers should be documented