-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackOfBoxes.java
78 lines (77 loc) · 2.54 KB
/
StackOfBoxes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayList;
import java.util.Collections;
class Box implements Comparable<Box>{
public int length;
public int width;
public int height;
public int area;
public Box(int length,int width,int height){
if(length > width){
this.length=length;
this.width=width;
}else{
this.length=width;
this.width=length;
}
this.height=height;
this.area=length*width;
}
public boolean canBePlacedOn(Box a){
return this.area < a.area;
}
@Override
public String toString(){
return this.height+","+this.width+","+this.length;
}
@Override
public int compareTo(Box a) {
return a.area - this.area;
}
}
public class StackOfBoxes{
public static int maxStackWithBase(ArrayList<Box> boxList, int index){
int maxHeight=0;
Box base=boxList.get(index);
for(int i=index+1;i<boxList.size();i++){
if(boxList.get(i).canBePlacedOn(base)){
int height=maxStackWithBase(boxList,i);
// System.out.println("base-->"+base+",box-->"+boxList.get(i));
maxHeight=Math.max(height, maxHeight);
}
}
maxHeight=base.height+maxHeight;
return maxHeight;
}
public static ArrayList<Box> generateNewList(ArrayList<Box> boxList){
ArrayList<Box> newList=new ArrayList<Box>();
for(int i=0;i<boxList.size();i++){
Box box=boxList.get(i);
newList.add(box);
newList.add(new Box(box.length,box.height,box.width));
newList.add(new Box(box.width,box.height,box.length));
}
Collections.sort(newList);
for(int i=0;i<newList.size();i++)
System.out.println(newList.get(i));
return newList;
}
public static int maxStackHeight(ArrayList<Box> boxList){
int maxHeight=0;
ArrayList<Box> newList=generateNewList(boxList);
for(int i=0;i<newList.size();i++){
int height=maxStackWithBase(newList,i);
//System.out.println("Height with base-->"+newList.get(i)+"is-->"+height);
maxHeight=Math.max(height, maxHeight);
}
return maxHeight;
}
public static void main(String[] args) {
ArrayList<Box> boxList=new ArrayList<Box>();
boxList.add(new Box(6,7,4));
boxList.add(new Box(2,3,1));
boxList.add(new Box(5,6,4));
boxList.add(new Box(12,32,10));
int height=maxStackHeight(boxList);
System.out.println(height);
}
}