【问题描述】[困难]
【解答思路】
1. 递归 动态规划
class Solution {
public int removeBoxes(int[] boxes) {
int[][][] dp = new int[100][100][100];
return calculatePoints(boxes, dp, 0, boxes.length - 1, 0);
}
public int calculatePoints(int[] boxes, int[][][] dp, int l, int r, int k) {
if (l > r) return 0;
if (dp[l][r][k] != 0) return dp[l][r][k];
//优化 找到相等的就切
while (r > l && boxes[r] == boxes[r - 1]) {
r--;
k++;
}
dp[l][r][k] = calculatePoints(boxes, dp, l, r - 1, 0) + (k + 1) * (k + 1);
for (int i = l; i < r; i++) {
if (boxes[i] == boxes[r]) {
dp[l][r][k] = Math.max(dp[l][r][k], calculatePoints(boxes, dp, l, i, k + 1) + calculatePoints(boxes, dp, i + 1, r - 1, 0));
}
}
return dp[l][r][k];
}
}
细节理解 通俗易解的图 要把中间那块抽出来 单独递归
图解二 宏观理解
【总结】
1. 递归+动态规划 锻炼抽象思维
2.难难难
转载链接:https://leetcode-cn.com/problems/remove-boxes/solution/yi-chu-he-zi-by-leetcode-solution/
参考链接:https://leetcode-cn.com/problems/remove-boxes/solution/guan-fang-fang-fa-2ji-yi-hua-sou-suo-dong-hua-tu-j/
参考链接:https://leetcode-cn.com/problems/remove-boxes/solution/guan-fang-ti-jie-de-tu-jie-by-xiayilive/