Skip to content

Commit 597d184

Browse files
committed
first try
1 parent 8bc5819 commit 597d184

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

main.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
def maxHeight(d1, d2, d3):
2+
"""
3+
A method that calculates largest possible tower height of given boxes (bad approach 1/2).
4+
Problem description: https://practice.geeksforgeeks.org/problems/box-stacking/1
5+
time complexity: O(n*max(max_d1, max_d2, max_d3)^2)
6+
space complexity: O(n*max(max_d1, max_d2, max_d3)^2)
7+
8+
Parameters
9+
----------
10+
d1 : int[]
11+
a list of int values representing the 1st dimension of a / multiple 3d-box(es)
12+
d2 : int[]
13+
a list of int values representing the 2nd dimension of a / multiple 3d-box(es)
14+
d3 : int[]
15+
a list of int values representing the 3rd dimension of a / multiple 3d-box(es)
16+
17+
Returns
18+
-------
19+
x : int
20+
the largest possible tower height
21+
"""
22+
23+
assert(len(d1) >= 1)
24+
assert(len(d1) == len(d2) == len(d3))
25+
26+
max_dimension = max([
27+
max(d1),
28+
max(d2),
29+
max(d3)
30+
])
31+
32+
boxes = zip(d1, d2, d3)
33+
boxes = map(sorted, boxes)
34+
boxes = sorted(boxes, key=lambda e: e[2])
35+
boxes = sorted(boxes, key=lambda e: e[1])
36+
boxes = sorted(boxes, key=lambda e: e[0])
37+
boxes = boxes + list(map(lambda e: [e[1], e[2], e[0]], boxes))
38+
boxes = sorted(boxes, key=lambda e: e[2])
39+
boxes = sorted(boxes, key=lambda e: e[1])
40+
boxes = sorted(boxes, key=lambda e: e[0])
41+
42+
n = len(boxes)
43+
44+
# dimension 1: i.th box
45+
# dimension 2: x-coordinate left
46+
# dimension 3: y-coordinate left
47+
max_height = {i: [[0 for _ in range(max_dimension + 1)]
48+
for _ in range(max_dimension + 1)] for i in range(-1, n)}
49+
50+
for i in range(n):
51+
box_x, box_y, box_z = boxes[i][0], boxes[i][1], boxes[i][2]
52+
for x in range(max_dimension + 1):
53+
for y in range(max_dimension + 1):
54+
max_tmp = max_height[i-1][x][y]
55+
if box_x <= x and box_y <= y:
56+
max_tmp = max(
57+
max_tmp, max_height[i-1][box_x-1][box_y-1] + box_z)
58+
if box_x <= x and box_z <= y:
59+
max_tmp = max(
60+
max_tmp, max_height[i-1][box_x-1][box_z-1] + box_y)
61+
if box_y <= x and box_z <= y:
62+
max_tmp = max(
63+
max_tmp, max_height[i-1][box_y-1][box_z-1] + box_x)
64+
max_height[i][x][y] = max_tmp
65+
return max_height[n-1][max_dimension][max_dimension]
66+

0 commit comments

Comments
 (0)