Created
August 17, 2014 22:23
-
-
Save chrislukkk/e5f3a99557876b12657d to your computer and use it in GitHub Desktop.
Career cup 9.10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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