-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcqq.cpp
More file actions
37 lines (36 loc) · 1.11 KB
/
Copy pathcqq.cpp
File metadata and controls
37 lines (36 loc) · 1.11 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
#include <bits/stdc++.h>
using namespace std;
// 戳气球
class Solution {
public:
int maxCoins(vector<int>& nums) {
const int size = nums.size();
nums.insert(nums.begin(), 1);
nums.push_back(1);
// dp[i][j]表示戳爆i和j之间的气球,最大的得分
int* dp[size+2];
for (int i = 0; i < size+2; i++)
dp[i] = new int[size+2-i]{0};
// 从下往上 从左到右遍历
for (int row = size; row >= 0; row--) {
for (int col = row+1; col < size+2; col++) {
int& m = dp[row][col-row];
// 遍历row和col之间的每一种戳破可能
for (int i = row+1; i < col; i++) {
m = max(m,
dp[row][i-row] + dp[i][col-i] +
nums[i]*nums[row]*nums[col]
);
}
}
}
int result = dp[0][size+1];
for (int i = 0; i < size+2; i++) delete[] dp[i];
return result;
}
};
int main() {
Solution s;
vector<int> ques{3,1,5,8};
cout << s.maxCoins(ques);
}