-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathRemove_Boxes.cpp
30 lines (28 loc) · 1.02 KB
/
Remove_Boxes.cpp
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
class Solution {
public:
int removeBoxes(vector<int>& boxes) {
const int n = (int)boxes.size();
if(n == 0) return 0;
// vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 0))); // MLE
int dp[n + 1][n + 1][n + 1] = {0};
for(int i = 0; i < n; i++) {
for(int k = 1; k <= i; k++) {
dp[i][i][k] = (k + 1) * (k + 1);
}
}
for(int len = 1; len <= n; len++) {
for(int i = 0; i <= n - len; i++) {
int j = i + len - 1;
for(int k = 0; k <= i; k++) {
dp[i][j][k] = (k + 1) * (k + 1) + dp[i + 1][j][0];
for(int l = i + 1; l <= j; l++) {
if(boxes[i] == boxes[l]) {
dp[i][j][k] = max(dp[i][j][k], dp[i + 1][l - 1][0] + dp[l][j][k + 1]);
}
}
}
}
}
return dp[0][n - 1][0];
}
};