-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathburst_ballons.cpp
More file actions
71 lines (66 loc) · 2.1 KB
/
burst_ballons.cpp
File metadata and controls
71 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
using namespace std;
/**
* Describe: 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.
* - 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
*
* Solution: Using the dynamic programming.
*/
class Solution {
public:
/**
* @param nums a list of integer
* @return an integer, maximum coins
*/
int maxCoins(vector<int>& nums) {
int len = nums.size();
vector<vector<int>> dp(len + 2, vector<int>(len + 2, 0));
int j = 0;
for (int step = 0; step < len; ++step) {
for (int i = 0; i < len - step; ++i) {
j = i + step;
int max = 0;
int cur = 0;
for (int idx = i; idx <= j; ++idx) {
cur = getMulti(nums, i - 1, idx, j + 1);
cur += dp[i + 1][idx] + dp[idx + 2][j + 1];
max = max > cur? max : cur;
}
dp[i + 1][j + 1] = max > dp[i + 1][j + 1]? \
max : dp[i + 1][j + 1];
}
}
return dp[1][len];
}
int getMulti(vector<int>& nums, int a, int b, int c) {
int tmp1, tmp2, tmp3;
tmp1 = a < 0? 1 : nums[a];
tmp2 = nums[b];
tmp3 = c >= static_cast<int>(nums.size())? 1 : nums[c];
return tmp1 * tmp2 * tmp3;
}
};
int main() {
Solution so;
vector<int> test;
int n, tmp;
while (cin >> n) {
test.clear();
while (n-- > 0) {
cin >> tmp;
test.push_back(tmp);
}
int re = so.maxCoins(test);
cout << re << endl;
}
return 0;
}