-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodinter9.10.java
54 lines (51 loc) · 1.24 KB
/
codinter9.10.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
import java.util.*;
public class CodInter {
public static void main(String[] argc) {
Box[] boxes = { new Box(5, 4, 8), new Box(2, 2, 2), new Box(7, 8, 3),new Box(7, 8, 1),new Box(3, 2, 1)};
ArrayList <Box> ans=createStack(boxes,-1);
System.out.println(heightOf(ans));
}
public static ArrayList <Box> createStack(Box box[],int bottom){
ArrayList <Box> al=new ArrayList <Box>();
int maxHeight=0;
ArrayList <Box> maxStack=new ArrayList<Box>();
for(int i=0;i<box.length;i++){
if(canBeAbove(bottom,i,box)){
ArrayList<Box> tempStack=createStack(box,i);
int temp=heightOf(tempStack);
if (temp>maxHeight){
maxHeight=temp;
maxStack=tempStack;
}
}
}
if(bottom!=-1){
al.add(box[bottom]);
al.addAll(maxStack);
}
else{
al.addAll(maxStack);
}
return al;
}
public static boolean canBeAbove(int bo,int i,Box[]box){
if(bo==-1) return true;
if(box[bo].depth>box[i].depth&&box[bo].width>box[i].width&&box[bo].height>box[i].height){
return true;
}
return false;
}
public static int heightOf(ArrayList <Box> stack){
int h=0;
for(Box box:stack){
h+=box.height;
}
return h;
}
}
class Box{
int depth;int height;int width;
public Box(int w,int h,int d){
depth=d;height=h;width=w;
}
}