Skip to content

Commit fc86d80

Browse files
authored
Merge pull request chipbk10#116 from chipbk10/Intervals
Solve Problem 546 - Remove Boxes
2 parents 8f47ed8 + 31ab7cb commit fc86d80

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package intervals;
2+
3+
public class Problem546_RemoveBoxes {
4+
5+
int n, dp[][][];
6+
7+
public int removeBoxes(int[] A) {
8+
n = A.length;
9+
dp = new int[n][n][100];
10+
return dfs(A, 0, n-1, 0);
11+
}
12+
13+
private int dfs(int[] A, int l, int r, int k) {
14+
if (l > r) return 0;
15+
if (dp[l][r][k] > 0) return dp[l][r][k];
16+
17+
dp[l][r][k] = dfs(A, l, r-1, 0) + (k+1)*(k+1);
18+
for (int i = l; i < r; i++) {
19+
if (A[i] == A[r]) {
20+
dp[l][r][k] = Math.max(dp[l][r][k], dfs(A,l,i,k+1) + dfs(A, i+1, r-1, 0));
21+
}
22+
}
23+
24+
return dp[l][r][k];
25+
}
26+
}

0 commit comments

Comments
 (0)