-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathboxStacking.py
More file actions
65 lines (48 loc) · 1.47 KB
/
Copy pathboxStacking.py
File metadata and controls
65 lines (48 loc) · 1.47 KB
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
class Box:
# Representation of a box
def __init__(self, h, w, d):
self.h = h
self.w = w
self.d = d
def __lt__(self, other):
return self.d * self.w < other.d * other.w
def maxStackHeight(arr, n):
rot = [Box(0, 0, 0) for _ in range(3 * n)]
index = 0
for i in range(n):
# Copy the original box
rot[index].h = arr[i].h
rot[index].d = max(arr[i].d, arr[i].w)
rot[index].w = min(arr[i].d, arr[i].w)
index += 1
# First rotation of the box
rot[index].h = arr[i].w
rot[index].d = max(arr[i].h, arr[i].d)
rot[index].w = min(arr[i].h, arr[i].d)
index += 1
rot[index].h = arr[i].d
rot[index].d = max(arr[i].h, arr[i].w)
rot[index].w = min(arr[i].h, arr[i].w)
index += 1
n *= 3
rot.sort(reverse = True)
msh = [0] * n
for i in range(n):
msh[i] = rot[i].h
for i in range(1, n):
for j in range(0, i):
if (rot[i].w < rot[j].w and
rot[i].d < rot[j].d):
if msh[i] < msh[j] + rot[i].h:
msh[i] = msh[j] + rot[i].h
maxm = -1
for i in range(n):
maxm = max(maxm, msh[i])
return maxm
# Driver Code
if __name__ == "__main__":
arr = [Box(4, 6, 7), Box(1, 2, 3),
Box(4, 5, 6), Box(10, 12, 32)]
n = len(arr)
print("The maximum possible height of stack is",
maxStackHeight(arr, n))