20190718
Description
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
Example:
Input: [3,1,5,8]
Output: 167
Explanation:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 315 + 358 + 138 + 181 = 167
Solution
The first sight to see this problem, an idea came to my mind that: it should be dynamic programming.
But first I choose recursion to solve this problem, which is really slow and stucked at 24 / 70 test case.
Below is my first version code:
class Solution {
public:
int maxCoins(vector<int>& nums) {
if(nums.size()==0) return 0;
if(nums.size()==1) return nums[0];
int mm = 0;
for(int i=0;i<nums.size();++i){
int n = nums[i];
// nums.erase(nums.begin()+i);
if (i==0){
int tmp = n*nums[1];
nums.erase(nums.begin()+i);
tmp += maxCoins(nums);
mm = max(tmp,mm);
nums.insert(nums.begin(),n);
}
else if(i==nums.size()-1){
int tmp = n*nums[i-1];
nums.erase(nums.begin()+i);
tmp += maxCoins(nums);
mm = max(tmp,mm);
nums.insert(nums.end(),n);
}
else{
int tmp = nums[i-1]*n*nums[i+1];
nums.erase(nums.begin()+i);
tmp += maxCoins(nums);
mm = max(tmp,mm);
nums.insert(nums.begin()+i,n);
}
}
return mm;
}
};
today I am a little bit lazy, so I turn to discussion, and found a elegant solution:
it split the problem into sub problems:
m
a
x
C
o
i
n
s
(
n
u
m
s
[
1
:
n
]
)
=
max
i
m
a
x
C
o
i
n
s
(
n
u
m
s
[
1
:
i
−
1
]
)
+
n
u
m
s
[
i
−
1
]
∗
+
m
a
x
C
o
i
n
s
(
n
u
m
s
[
i
+
1
:
n
]
)
maxCoins(nums[1:n])=\max_{i} maxCoins(nums[1:i-1]) + nums[i-1]* +maxCoins(nums[i+1:n])
maxCoins(nums[1:n])=maximaxCoins(nums[1:i−1])+nums[i−1]∗+maxCoins(nums[i+1:n])
which means, it recurses on the last burst.
Below is the cpp code:
int maxCoins(vector<int>& nums) {
int N = nums.size();
nums.insert(nums.begin(), 1);
nums.insert(nums.end(), 1);
// rangeValues[i][j] is the maximum # of coins that can be obtained
// by popping balloons only in the range [i,j]
vector<vector<int>> rangeValues(nums.size(), vector<int>(nums.size(), 0));
// build up from shorter ranges to longer ranges
for (int len = 1; len <= N; ++len) {
for (int start = 1; start <= N - len + 1; ++start) {
int end = start + len - 1;
// calculate the max # of coins that can be obtained by
// popping balloons only in the range [start,end].
// consider all possible choices of final balloon to pop
int bestCoins = 0;
for (int final = start; final <= end; ++final) {
int coins = rangeValues[start][final-1] + rangeValues[final+1][end]; // coins from popping subranges
coins += nums[start-1] * nums[final] * nums[end+1]; // coins from final pop
if (coins > bestCoins) bestCoins = coins;
}
rangeValues[start][end] = bestCoins;
}
}
return rangeValues[1][N];
}
tbc