|
| 1 | +/* |
| 2 | +https://www.geeksforgeeks.org/box-stacking-problem-dp-22/ |
| 3 | +
|
| 4 | +Question is on the same lines as the Longest Increasing Subsequence |
| 5 | +*/ |
| 6 | + |
| 7 | +class Box implements Comparable<Box>{ |
| 8 | + |
| 9 | + // h --> height, w --> width, d --> depth |
| 10 | + int h, w, d, area; |
| 11 | + |
| 12 | + // for simplicity of solution, |
| 13 | + // always keep w <= d |
| 14 | + |
| 15 | + /*Constructor to initialise object*/ |
| 16 | + public Box(int h, int w, int d) { |
| 17 | + this.h = h; |
| 18 | + this.w = w; |
| 19 | + this.d = d; |
| 20 | + } |
| 21 | + |
| 22 | + /*To sort the box array on the basis |
| 23 | + of area in decreasing order of area */ |
| 24 | + public int compareTo(Box o) { |
| 25 | + return o.area-this.area; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + //DYNAMIC PROGRAMMING - BOTTOM UP APPROACH |
| 30 | + class BoxStack{ |
| 31 | + public int maxHeight(Box b[]){ |
| 32 | + int n = b.length; |
| 33 | + Box allBoxes[] = new Box[n*3]; |
| 34 | + |
| 35 | + //creating all rotations of a box |
| 36 | + for(int i=0 ; i < n ; i++){ |
| 37 | + Box box = b[i]; |
| 38 | + allBoxes[i*3] = new Box(box.h, Math.max(box.d, box.w), Math.min(box.d, box.w)); |
| 39 | + allBoxes[i*3 + 1] = new Box(box.d, Math.max(box.h, box.w), Math.min(box.h, box.w)); |
| 40 | + allBoxes[i*3 + 2] = new Box(box.w, Math.max(box.d, box.h), Math.min(box.d, box.h)); |
| 41 | + } |
| 42 | + |
| 43 | + //calculating area of the boxes - we need this because each box must have smaller length and breadth than the box below it |
| 44 | + //this helps us to explore only valid subproblems - explore the boxes having greater dimensions than the current one |
| 45 | + for(int i=0 ; i < n*3 ; i++) allBoxes[i].area = allBoxes[i].d * allBoxes[i].w; |
| 46 | + |
| 47 | + //sorting in descending order according to the area |
| 48 | + Arrays.sort(allBoxes); |
| 49 | + |
| 50 | + //msh[i] indicates the maximum possible height of a stack when box i is at the top |
| 51 | + int msh[] = new int[n*3]; |
| 52 | + |
| 53 | + //storing the current height of each of the box |
| 54 | + for(int i=0 ; i < n*3 ; i++) msh[i] = allBoxes[i].h; |
| 55 | + |
| 56 | + //for each of the box, we compute the maximum stack height when it is at the top |
| 57 | + for(int i=0 ; i < n*3 ; i++){ |
| 58 | + |
| 59 | + Box topBox = allBoxes[i]; |
| 60 | + |
| 61 | + //initializing maximum stack height of the subproblem |
| 62 | + int maxH = 0; |
| 63 | + |
| 64 | + //SUBPROBLEMS - explore all boxes greater in dimension than the current one to find the one giving the maximum height |
| 65 | + for(int j=0 ; j < i ; j++){ |
| 66 | + |
| 67 | + Box bottomBox = allBoxes[j]; |
| 68 | + |
| 69 | + //Constraints - greater dimensions than the current box |
| 70 | + if(topBox.w < bottomBox.w && topBox.d < bottomBox.d){ |
| 71 | + //Out of all the subproblems consider the one giving max height |
| 72 | + //This box comes below the current box |
| 73 | + maxH = Math.max(maxH, msh[j]); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + //Store the result - add the height of the box to the calculated max height from the subproblems |
| 78 | + msh[i] = maxH + topBox.h; |
| 79 | + } |
| 80 | + |
| 81 | + int maxstackHeight = -1; |
| 82 | + for(int i=0 ; i < n*3 ; i++){ |
| 83 | + if(maxstackHeight < msh[i]) maxstackHeight = msh[i]; |
| 84 | + } |
| 85 | + |
| 86 | + return maxstackHeight; |
| 87 | + |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /* |
| 93 | + Time Complexity - O(n^2) |
| 94 | + Space Complexity - O(n) |
| 95 | + */ |
| 96 | + |
| 97 | + |
0 commit comments