MCPcopy
hub / github.com/careercup/ctci / createStackDP

Method createStackDP

java/Chapter 9/Question9_10/Question.java:43–70  ·  view source on GitHub ↗
(Box[] boxes, Box bottom, HashMap<Box, ArrayList<Box>> stack_map)

Source from the content-addressed store, hash-verified

41 }
42
43 public static ArrayList<Box> createStackDP(Box[] boxes, Box bottom, HashMap<Box, ArrayList<Box>> stack_map) {
44 if (bottom != null && stack_map.containsKey(bottom)) {
45 return (ArrayList<Box>) stack_map.get(bottom).clone();
46 }
47
48 int max_height = 0;
49 ArrayList<Box> max_stack = null;
50 for (int i = 0; i < boxes.length; i++) {
51 if (boxes[i].canBeAbove(bottom)) {
52 ArrayList<Box> new_stack = createStackDP(boxes, boxes[i], stack_map);
53 int new_height = stackHeight(new_stack);
54 if (new_height > max_height) {
55 max_stack = new_stack;
56 max_height = new_height;
57 }
58 }
59 }
60
61 if (max_stack == null) {
62 max_stack = new ArrayList<Box>();
63 }
64 if (bottom != null) {
65 max_stack.add(0, bottom);
66 }
67 stack_map.put(bottom, max_stack);
68
69 return max_stack;
70 }
71
72
73 public static void main(String[] args) {

Callers 1

mainMethod · 0.95

Calls 6

stackHeightMethod · 0.95
canBeAboveMethod · 0.80
putMethod · 0.80
cloneMethod · 0.45
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected