9.10 叠箱子,不是很理解,建议做几道dp的leetcode题目

You have a stack of n boxes, with widths w., heights l\ and depths dr The boxes
cannot be rotated and can only be stacked on top of one another if each box in the
stack is strictly larger than the box above it in width, height, and depth. Implement
a method to build the tallest stack possible, where the heigh t of a stack is the sum of
the heights of each box. 答案cc150 165页



public ArrayList<Box> createStackDP(Box[] boxes, Box bottom,

2 HashMap<Box, ArrayList<Box» stack_map) {
3 if (bottom != null && stack_map.containsKey(bottom)) {
4 return stack_map.get(bottom);
5 }
6
int max_height = 0;
8 ArrayList<Box> max_stack = null;
9 for (int i = 0; i < boxes.length; i++) {
10 if (boxes[i].canBeAbove(bottom)) {
11 ArrayList<Box> new_stack =
12 createStackDP(boxes, boxes[i], stackjnap);
13 int new_height = stackHeight(new_stack);
14 if (newjieight > max_height) {
15 max_stack = new_stack;
16 max_height = newjieight;
17 }
18 }
19 }
20
21 if (max_stack == null) max_stack = new ArrayList<Box>();
22 if (bottom != null) max_stack.add(0, bottom);
23 stackjnap.put(bottomj max_stack);
24
25 return (ArrayList<Box>)max_stack.clone();
26 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值