-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path312.cpp
More file actions
20 lines (20 loc) · 693 Bytes
/
312.cpp
File metadata and controls
20 lines (20 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int maxCoins(vector<int>& nums) {
int n = nums.size();
nums.insert(nums.begin(), 1);
nums.push_back(1);
vector<vector<int>> dp(n + 2, vector<int>(n + 2, 0));
for (int len = 1; len <= n; ++len) {
for (int i = 1, j = i + len - 1; j <= n; ++i, ++j) {
if (i == j) dp[i][j] = nums[i - 1] * nums[i] * nums[i + 1];
else {
for (int k = i; k <= j; ++k) {
dp[i][j] = max(dp[i][j], nums[i - 1] * nums[k] * nums[j + 1] + dp[i][k - 1] + dp[k + 1][j]);
}
}
}
}
return dp[1][n];
}
};