[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] Longest Increasing Subsequence

[Coding Made Simple] Maximum Sum Increasing Subsequence

posted @ 2017-08-25 12:17  Review->Improve  阅读(393)  评论(0编辑  收藏  举报