题目:
题解:
class Solution {int[][][] dp;public int removeBoxes(int[] boxes) {int length = boxes.length;dp = new int[length][length][length];return calculatePoints(boxes, 0, length - 1, 0);}public int calculatePoints(int[] boxes, int l, int r, int k) {if (l > r) {return 0;}if (dp[l][r][k] == 0) {int r1 = r, k1 = k;while (r1 > l && boxes[r1] == boxes[r1 - 1]) {r1--;k1++;}dp[l][r][k] = calculatePoints(boxes, l, r1 - 1, 0) + (k1 + 1) * (k1 + 1);for (int i = l; i < r1; i++) {if (boxes[i] == boxes[r1]) {dp[l][r][k] = Math.max(dp[l][r][k], calculatePoints(boxes, l, i, k1 + 1) + calculatePoints(boxes, i + 1, r1 - 1, 0));}}}return dp[l][r][k];}
}