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

本文介绍了一种使用动态规划方法来寻找最高堆叠箱组合的算法实现。该算法通过递归方式检查每个箱子是否可以放置在另一个箱子之上,并记录最大高度的堆叠方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值