8.13 Stack of Boxes
来源:互联网 发布:算法导论 第三版 目录 编辑:程序博客网 时间:2024/06/06 17:38
First we have to sort the boxes by their height (descending), then we try to find each the max height for each box i as the bottom one and store their max height in a int[].
public static int createStack(ArrayList<Box> boxes) { Collections.sort(boxes, new BoxComparator()); int maxHeight = 0; int[] mp = new int[boxes.size()]; for (int i = 0; i < boxes.size(); i++) { int height = createStack(boxes, i,mp); maxHeight = Math.max(maxHeight, height); } return maxHeight; } public static int createStack(ArrayList<Box> boxes, int bottomIndex,int[] mp) { if(bottomIndex<boxes.size() && mp[bottomIndex] > 0) return mp[bottomIndex]; Box bottom = boxes.get(bottomIndex); int maxHeight = 0; for (int i = bottomIndex + 1; i < boxes.size(); i++) { if (boxes.get(i).canBeAbove(bottom)) { int height = createStack(boxes, i,mp); maxHeight = Math.max(height, maxHeight); } } maxHeight += bottom.height; mp[bottomIndex] = maxHeight; return maxHeight; } public static void main(String[] args) { Box[] boxList = { new Box(6, 4, 4), new Box(8, 6, 2), new Box(5, 3, 3), new Box(7, 8, 3), new Box(4, 2, 2), new Box(9, 7, 3)}; ArrayList<Box> boxes = new ArrayList<Box>(); for (Box b : boxList) { boxes.add(b); } int height = createStack(boxes); System.out.println(height); }
0 0
- 8.13 Stack of Boxes
- UVa 10489 - Boxes of Chocolates
- Boxes of Chocolates Again UVA
- Three Types of Dialog Boxes in JavaScript
- uva 10590 Boxes of Chocolates Again
- UVa:10590 Boxes of Chocolates Again
- UVa-10590-Boxes of Chocolates Again
- 【简单dp】Boxes of Chocolates Again
- Boxes
- stack of gcc
- The using of stack
- The operate of stack
- UVA120 Stack of Flapjacks
- the analysis of some mp4 container boxes(stsd/avcC/stco...)
- uva-120 stack of flapjacks
- The express of stack squenced
- stack of flapjacks uva 120
- IO stack of Linux kernel
- Laravel homestead 安装与使用 From Scratch
- C++ Boost在Windows和Linux下的编译安装
- MATLAB ROBOT里的link robot fkine drivebot
- R table()&cbind() Fuction
- 数据库应用-NF^2 Modell(Non First Normal Form)
- 8.13 Stack of Boxes
- 各品牌进入启动菜单选择项的快捷键
- IOS fatal error: unexpectedly found nil while unwrapping an Optional value的一个原因
- R Global Option Settings
- [leetcode] 90. Subsets II 解题报告
- 8.14 Boolean Evaluation
- git 学习笔记3
- 最短路径__Silver Cow Party ( Poj 3268 )
- Android内存泄露案例分析