长宽高都更小时箱子才能往上堆,最高能堆多高

先把箱子按某一维(如height)从大到小排,子问题boxes[idx, ...]考虑堆不堆第idx箱子。

// 子问题boxes[idx,...]可堆可不堆第idx箱子,memo[idx]记录以第idx箱子为底最高能堆多高
int createStack(ArrayList<Box> boxes, int idx, Box prev, int[] memo) {
    if (idx >= boxes.size()) return 0;

    Box curr = boxes.get(idx);
    int heightWithCurr = 0;
    if (prev == null || curr.canAbove(prev)) { // 堆第idx箱子
        if (memo[idx] == 0) memo[idx] = curr.height + createStack(boxes, idx + 1, curr, memo);
        heightWithCurr = memo[idx];
    }
    // 不堆第idx箱子
    int heightWithoutCurr = createStack(boxes, idx + 1, prev, memo);
    return Math.max(heightWithCurr, heightWithoutCurr);
}