Skip to content

Commit b278b2f

Browse files
committed
New Problem "Burst Balloons"
1 parent cb76a75 commit b278b2f

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LeetCode
1414
|322|[Coin Change](https://leetcode.com/problems/coin-change/) | [C++](./algorithms/cpp/coinChange/coinChange.cpp)|Medium|
1515
|319|[Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) | [C++](./algorithms/cpp/bulbSwitcher/bulbSwitcher.cpp)|Medium|
1616
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [C++](./algorithms/cpp/countOfSmallerNumbersAfterSelf/countOfSmallerNumbersAfterSelf.cpp)|Hard|
17+
|312|[Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [C++](./algorithms/cpp/burstBalloons/BurstBalloons.cpp)|Hard|
1718
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [C++](./algorithms/cpp/rangeSumQuery-Immutable/rangeSumQuery-Mutable/RangeSumQueryMutable.cpp)|Medium|
1819
|306|[Additive Number](https://leetcode.com/problems/additive-number/) | [C++](./algorithms/cpp/additiveNumber/AdditiveNumber.cpp)|Medium|
1920
|304|[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | [C++](./algorithms/cpp/rangeSumQuery2D-Immutable/RangeSumQuery2dImmutable.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Source : https://leetcode.com/problems/burst-balloons/
2+
// Author : Hao Chen
3+
// Date : 2016-01-17
4+
5+
/***************************************************************************************
6+
*
7+
* Given n balloons, indexed from 0 to n-1. Each balloon is painted with a
8+
* number on it represented by array nums.
9+
*
10+
* You are asked to burst all the balloons. If the you burst
11+
* balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left
12+
* and right are adjacent indices of i. After the burst, the left and right
13+
* then becomes adjacent.
14+
*
15+
* Find the maximum coins you can collect by bursting the balloons wisely.
16+
*
17+
* Note:
18+
* (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can
19+
* not burst them.
20+
* (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
21+
*
22+
* Example:
23+
*
24+
* Given [3, 1, 5, 8]
25+
*
26+
* Return 167
27+
*
28+
* nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
29+
* coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
30+
*
31+
* Credits:Special thanks to @dietpepsi for adding this problem and creating all test
32+
* cases.
33+
***************************************************************************************/
34+
35+
36+
37+
class Solution {
38+
public:
39+
int maxCoins(vector<int>& nums) {
40+
//remove all of zero item
41+
nums.erase(remove_if(nums.begin(), nums.end(), [](int n){return n==0;}), nums.end());
42+
43+
//add 1 for head and tail
44+
nums.insert(nums.begin(),1);
45+
nums.push_back(1);
46+
47+
int n = nums.size();
48+
vector< vector<int> > matrix(n, vector<int>(n,0));
49+
50+
return maxCoins_DP(nums, matrix);
51+
return maxCoins_DC(nums, matrix, 0, n-1);
52+
}
53+
54+
55+
//Divide and Conquer
56+
//
57+
// If we seprate the array to two part, left part and right part.
58+
//
59+
// Then, we will find in this problem the left and right become adjacent
60+
// and have effects on the maxCoins in the future.
61+
//
62+
// So, if we think reversely, if the balloon i is the last balloon of all to burst,
63+
// the left and right section now has well defined boundary and do not affect each other!
64+
// Therefore we can do either recursive method with memoization
65+
//
66+
int maxCoins_DC(vector<int>& nums, vector<vector<int>>& matrix, int low, int high) {
67+
if (low + 1 == high) return 0;
68+
if (matrix[low][high] > 0) return matrix[low][high];
69+
int result = 0;
70+
for (int i = low + 1; i < high; ++i){
71+
result = max(result, nums[low] * nums[i] * nums[high]
72+
+ maxCoins_DC(nums, matrix, low, i)
73+
+ maxCoins_DC(nums, matrix, i, high));
74+
}
75+
matrix[low][high] = result;
76+
return result;
77+
}
78+
79+
//Dynamic Programming
80+
//
81+
// using the same idea of above
82+
//
83+
int maxCoins_DP(vector<int>& nums, vector<vector<int>>& dp) {
84+
int n = nums.size();
85+
for (int k = 2; k < n; ++k) {
86+
for (int low = 0; low < n - k; low++){
87+
int high = low + k;
88+
for (int i = low + 1; i < high; ++i)
89+
dp[low][high] = max( dp[low][high],
90+
nums[low] * nums[i] * nums[high] + dp[low][i] + dp[i][high]);
91+
}
92+
}
93+
return dp[0][n - 1];
94+
}
95+
96+
private:
97+
void printVector(vector<int>& nums) {
98+
cout << "nums: ";
99+
for (auto n: nums) {
100+
cout << n << ' ';
101+
}
102+
cout << '\n';
103+
}
104+
};

0 commit comments

Comments
 (0)