[Coding Made Simple] Box Stacking
Posted Push your limit!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Coding Made Simple] Box Stacking相关的知识,希望对你有一定的参考价值。
Given boxes of different dimensions, stack them on top of each other to get maximum height such that box on top has strictly less length and width than box under it.
Algorithm.
1. get all the box permutation with each permutation\'s length >= width; so if given n boxes, we\'ll get 3 * n boxes.
2. sort all the permutation boxes based the box area(length * width) in descending order.
Because only boxes with smaller area can possibly be on top of boxes with bigger area, sorting in this way reduces this problem
to a problem similar with Maximum Sum Increasing Subsequence. In this problem, Maximum Height Area Increasing Boxes.
3. Create maxHeight[], maxHeight[i] is the max height possible out of box[0.....i]. To compute each maxHeight[i], check all boxes
whose area is bigger than boxes[i] to see if boxes[i] can be on top of boxes[j], j is from 0 to i - 1.
If it can, update maxHeight[i] if necessary.
4. Find the max value of maxHeight[i] as the final answer.
Similarly with Maximum Sum Increasing Subsequence, the above algorithm has overlapping subproblems issue.
Dynamic Programming Solution.
1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.Comparator; 4 5 class Box { 6 int length; 7 int width; 8 int height; 9 Box(int l, int w, int h) { 10 length = l; 11 width = w; 12 height = h; 13 } 14 void display() { 15 System.out.println("length: " + length + " , width: " + width + " , height: " + height); 16 } 17 } 18 public class BoxStacking { 19 private ArrayList<Box> boxStack; 20 public int maxHeightOfStackingBox(Box[] boxes) { 21 boxStack = new ArrayList<Box>(); 22 if(boxes == null || boxes.length == 0) { 23 return 0; 24 } 25 Box[] boxPermu = getBoxPermutation(boxes); 26 int[] maxHeight = new int[boxPermu.length]; 27 int[] boxIdx = new int[boxPermu.length]; 28 for(int i = 0; i < boxPermu.length; i++) { 29 maxHeight[i] = boxPermu[i].height; 30 boxIdx[i] = i; 31 } 32 for(int i = 1; i < boxPermu.length; i++) { 33 for(int j = 0; j < i; j++) { 34 if(boxPermu[j].length > boxPermu[i].length && boxPermu[j].width > boxPermu[i].width) { 35 if(maxHeight[j] + boxPermu[i].height > maxHeight[i]) { 36 maxHeight[i] = maxHeight[j] + boxPermu[i].height; 37 boxIdx[i] = j; 38 } 39 40 } 41 } 42 } 43 int maxH = 0; int topBoxIdx = -1; 44 for(int i = 0; i < boxPermu.length; i++) { 45 if(maxHeight[i] > maxH) { 46 maxH = maxHeight[i]; 47 topBoxIdx = i; 48 } 49 } 50 while(topBoxIdx != boxIdx[topBoxIdx]) { 51 boxStack.add(boxPermu[topBoxIdx]); 52 topBoxIdx = boxIdx[topBoxIdx]; 53 } 54 boxStack.add(boxPermu[topBoxIdx]); 55 return maxH; 56 } 57 private Box[] getBoxPermutation(Box[] boxes) { 58 Box[] boxPermu = new Box[boxes.length * 3]; 59 int idx = 0; 60 for(int i = 0; i < boxes.length; i++) { 61 boxPermu[idx++] = new Box(Math.max(boxes[i].length, boxes[i].width), 62 Math.min(boxes[i].length, boxes[i].width), 63 boxes[i].height); 64 boxPermu[idx++] = new Box(Math.max(boxes[i].length, boxes[i].height), 65 Math.min(boxes[i].length, boxes[i].height), 66 boxes[i].width); 67 boxPermu[idx++] = new Box(Math.max(boxes[i].height, boxes[i].width), 68 Math.min(boxes[i].height, boxes[i].width), 69 boxes[i].length); 70 } 71 Comparator<Box> comp = new Comparator<Box>() { 72 public int compare(Box b1, Box b2) { 73 return b2.length * b2.width - b1.length * b1.width; 74 } 75 76 }; 77 Arrays.sort(boxPermu, comp); 78 return boxPermu; 79 } 80 public static void main(String[] args) { 81 Box box1 = new Box(1, 2, 4); 82 Box box2 = new Box(3, 2, 5); 83 Box[] boxes = {box1, box2}; 84 BoxStacking test = new BoxStacking(); 85 System.out.println(test.maxHeightOfStackingBox(boxes)); 86 for(int i = 0; i < test.boxStack.size(); i++) { 87 test.boxStack.get(i).display(); 88 } 89 } 90 }
Related Problems
以上是关于[Coding Made Simple] Box Stacking的主要内容,如果未能解决你的问题,请参考以下文章
[Coding Made Simple] Longest Increasing Subsequence
[Coding Made Simple] String Interleaving
[Coding Made Simple] Matrix Chain Multiplication
[Coding Made Simple] Burst Balloon