-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBurstBallons.cpp
More file actions
55 lines (45 loc) · 1010 Bytes
/
BurstBallons.cpp
File metadata and controls
55 lines (45 loc) · 1010 Bytes
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
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <bitset>
#include <map>
using namespace std;
class Solution {
private:
int A[502][502];
public:
void calculate(vector<int>& nums, int lo, int hi){
int max = 0;
for(int i=lo; i<=hi; i++){
int this_run = get(nums, lo, i-1)
+ get(nums, i+1, hi)
+ nums[i]*nums[lo-1]*nums[hi+1];
if(this_run > max)
max = this_run;
}
// cout << lo << ", " << hi << ": " << max << endl;
A[lo][hi] = max;
}
int get(vector<int>& nums, int lo, int hi){
if(lo > hi)
return 0;
if(A[lo][hi] == -1)
calculate(nums, lo, hi);
return A[lo][hi];
}
int maxCoins(vector<int>& nums) {
if(nums.size() == 0)
return 0;
memset(A, -1, sizeof(A));
nums.insert(nums.begin(), 1);
nums.push_back(1);
return get(nums, 1, nums.size()-2);
}
};
int main(int argc, char *argv[]){
Solution sol;
vector<int> nums{3, 1, 5, 8};
cout << sol.maxCoins(nums) << endl;
return 0;
}