Skip to content

Instantly share code, notes, and snippets.

@chrislukkk
Created August 17, 2014 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislukkk/e5f3a99557876b12657d to your computer and use it in GitHub Desktop.
Save chrislukkk/e5f3a99557876b12657d to your computer and use it in GitHub Desktop.
Career cup 9.10
package Chapter9;
import java.util.ArrayList;
import java.util.HashMap;
public class Box {
public int w;
public int h;
public int d;
public Box(int w, int h, int d) {
this.w = w;
this.h = h;
this.d = d;
}
public static ArrayList<Box> getMaxHeightStack(ArrayList<Box> boxes,
Box bottom, HashMap<Box, ArrayList<Box>> stackCache) {
//check cache
if (stackCache.containsKey(bottom))
return stackCache.get(bottom);
int maxHeight = 0;
ArrayList<Box> maxHeightStack = new ArrayList<>();
for (Box box : boxes) {
if(box.canPutOn(bottom)) {
ArrayList<Box> s = getMaxHeightStack(boxes, box, stackCache);
int height = getStackHeight(s);
if(maxHeight < height) {
maxHeight = height;
maxHeightStack = s;
}
}
}
maxHeightStack.add(0, bottom);
stackCache.put(bottom, maxHeightStack);
return maxHeightStack;
}
private static int getStackHeight(ArrayList<Box> s) {
int height = 0;
for (Box box : s) {
height += box.h;
}
return height;
}
private boolean canPutOn(Box bottom) {
return bottom.h > h && bottom.w > w && bottom.d > d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment