9.10 叠箱子,不是很理解,建议做几道dp的leetcode题目
2015-02-11 06:28
381 查看
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 }
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 }
相关文章推荐
- 9.10 叠箱子 ,建议看leetcode dp部分
- leetcode -- Unique Paths I &&II-- 典型DP 题目,简单要看
- poj1036-题目好难理解,理解了就变得简单了许多dp
- leetcode -- Decode Ways -- dp题目
- leetcode题目:gas-station 使用start/end方法的个人理解
- hdu3280(不是区间DP的解法 很好理解)
- [LeetCode]10. Regular Expression Matching的DP理解及代码
- leetcode -- Triangle -- dp题目重点
- LeetCode上的几道数据库题目
- leetcode 546. Remove Boxes 删除箱子获得最大的分数 + 很复杂的DP + z做不出来
- dp题目思路理解 (嵌套镶嵌问题DAG)
- 几道简单dp题目。
- LeetCode中几道链表反转相关题目(Reorder List、Rotate List、Reverse Nodes in k-Group)
- leetcode -- Range Sum Query 2D - Immutable -- 简单DP题目
- leetcode中的DP题目总结
- 百度几道面试题目
- 在asp.net中强烈建议通过参数来实现sql而不是sql拼接
- 关于查找 “选修了全部课程的学生姓名”题目的理解--嘟嘟胖
- 几道c语言的题目
- 部门培训阶段考试题目,你会几道?