## Sunday, July 3, 2016

### Stack of Boxes

Related: Dynamic Programming | Set 22 (Box Stacking Problem)
https://github.com/wzhishen/cracking-the-coding-interview/blob/master/src/chap09/Q10.java
You have a stack of n boxes, with widths w1 , heights h i, and depths di . 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 compute the
height of the tallest possible stack. The height of a stack is the sum of the heights of each box.

int createStack(ArrayList<Box> boxes) {
Collections.sort(boxes, new BoxComparator());
int[] stackMap = new int[boxes.size()];
return createStack(boxes, null, 0, stackMap);
}
int createStack(ArrayList<Box> boxes, Box bottom, int offset, int[] stackMap) {
if (offset >= boxes.size()) return 0; // Base case

/* height with this bottom*/
Box newBottom = boxes.get(offset);
int heightWithBottom = 0;
if (bottom==null || newBottom.canBeAbove(bottom)) {
if (stackMap[offset] == 0) {
stackMap[offset] = createStack(boxes, newBottom, offset+ 1, stackMap);
stackMap[offset] += newBottom.height;
}
heightWithBottom = stackMap[offset];
}

/* without this bottom*/
int heightWithoutBottom createStack(boxes, bottom, offset+ 1, stackMap);
/* Return better of two options. */
return Math.max(heightWithBottom, heightWithoutBottom);
}

X. With Cache
We try to find the best solution that looks like {b3,b4, ••• } even though we may have already found the best solution with b4 at the bottom. Instead of
generating these solutions from scratch, we can cache these results using memoization.
int createStack(ArrayList<Box> boxes) {
Collections.sort(boxes, new BoxComparator());
int maxHeight = 0;
int[] stackMap = new int[boxes.size()];
for (int i= 0; i < boxes.size(); i++) {
int height = createStack(boxes, i, stackMap);
maxHeight = Math.max(maxHeight, height);
}
return maxHeight;
}

int createStack(ArrayList<Box> boxes, int bottomindex, int[] stackMap) {
if (bottomIndex < boxes.size() && stackMap[bottomindex] > 0) {
return stackMap[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, stackMap);
maxHeight = Math.max(height, maxHeight);
}
}
maxHeight += bottom.height;
stackMap[bottomindex] = maxHeight;
return maxHeight;
}

X.
int createStack(ArrayList<Box> boxes) {
/* Sort in decending order by height. */
Collections.sort(boxes, new BoxComparator());
int maxHeight = 0;
for (int i= 0; i < boxes.size(); i++) {
int height = createStack(boxes, i);
maxHeight = Math.max(maxHeight, height);
}
return maxHeight;
}

int createStack(ArrayList<Box> boxes, int bottomindex) {
Box bottom = boxes.get(bottomindex);
int maxHeight = 0;
for (inti= bottomlndex + 1; i < boxes.size(); i++) {
if (boxes.get(i).canBeAbove(bottom)) {
int height= createStack(boxes, i);
maxHeight = Math.max(height, maxHeight);
}
}
maxHeight T= bottom.height;
return maxHeight;
}

class BoxComparator implements Comparator<Box> {
@Override
public int compare(Box x, Box y){
return y.height - x.height;
}
}
public static ArrayList<Box> buildTallestStack(Box[] boxes) {
if (boxes == null) return null;
return buildTallestStack(boxes, null);
}

private static ArrayList<Box> buildTallestStack(Box[] boxes, Box bottom) {
int maxHeight = 0;
ArrayList<Box> maxStack = null;
for (Box box : boxes) {
if (box.canPlaceAbove(bottom)) {
ArrayList<Box> boxStack = buildTallestStack(boxes, box);
int height = getStackHeight(boxStack);
if (height > maxHeight) {
maxHeight = height;
maxStack = boxStack;
}
}
}
if (maxStack == null) maxStack = new ArrayList<Box>();
if (bottom != null) maxStack.add(0, bottom);
return maxStack;
}

private static int getStackHeight(List<Box> boxes) {
int height = 0;
for (Box b : boxes) height += b.height;
return height;
}

public static class Box {
private int width, length, height;
public Box(int w, int l, int h) {
width = w;
length = l;
height = h;
}
public boolean canPlaceAbove(Box b) {
return b == null ||
(this.width < b.width &&
this.length < b.length &&
this.height < b.height);
}
public String toString() {
return "(" + width + ", " + length + ", " + height + ")";
}
}