-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
202 lines (171 loc) · 6.87 KB
/
main.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
def maxHeightTry1(d1, d2, d3):
"""
A method that calculates largest possible tower height of given boxes (bad approach 1/2).
Problem description: https://practice.geeksforgeeks.org/problems/box-stacking/1
time complexity: O(n*max(max_d1, max_d2, max_d3)^2)
space complexity: O(n*max(max_d1, max_d2, max_d3)^2)
Parameters
----------
d1 : int[]
a list of int values representing the 1st dimension of a / multiple 3d-box(es)
d2 : int[]
a list of int values representing the 2nd dimension of a / multiple 3d-box(es)
d3 : int[]
a list of int values representing the 3rd dimension of a / multiple 3d-box(es)
Returns
-------
x : int
the largest possible tower height
"""
assert(len(d1) >= 1)
assert(len(d1) == len(d2) == len(d3))
max_dimension = max([
max(d1),
max(d2),
max(d3)
])
boxes = zip(d1, d2, d3)
boxes = map(sorted, boxes)
boxes = sorted(boxes, key=lambda e: e[2])
boxes = sorted(boxes, key=lambda e: e[1])
boxes = sorted(boxes, key=lambda e: e[0])
boxes = boxes + list(map(lambda e: [e[1], e[2], e[0]], boxes))
boxes = sorted(boxes, key=lambda e: e[2])
boxes = sorted(boxes, key=lambda e: e[1])
boxes = sorted(boxes, key=lambda e: e[0])
n = len(boxes)
# dimension 1: i.th box
# dimension 2: x-coordinate left
# dimension 3: y-coordinate left
max_height = {i: [[0 for _ in range(max_dimension + 1)]
for _ in range(max_dimension + 1)] for i in range(-1, n)}
for i in range(n):
box_x, box_y, box_z = boxes[i][0], boxes[i][1], boxes[i][2]
for x in range(max_dimension + 1):
for y in range(max_dimension + 1):
max_tmp = max_height[i-1][x][y]
if box_x <= x and box_y <= y:
max_tmp = max(
max_tmp, max_height[i-1][box_x-1][box_y-1] + box_z)
if box_x <= x and box_z <= y:
max_tmp = max(
max_tmp, max_height[i-1][box_x-1][box_z-1] + box_y)
if box_y <= x and box_z <= y:
max_tmp = max(
max_tmp, max_height[i-1][box_y-1][box_z-1] + box_x)
max_height[i][x][y] = max_tmp
return max_height[n-1][max_dimension][max_dimension]
def maxHeightTry2(d1, d2, d3):
"""
A method that calculates largest possible tower height of given boxes (optimized, but bad approach 2/2).
Problem description: https://practice.geeksforgeeks.org/problems/box-stacking/1
time complexity: O(n*max(max_d1, max_d2, max_d3)^2)
space complexity: O(max(max_d1, max_d2, max_d3)^2)
Parameters
----------
d1 : int[]
a list of int values representing the 1st dimension of a / multiple 3d-box(es)
d2 : int[]
a list of int values representing the 2nd dimension of a / multiple 3d-box(es)
d3 : int[]
a list of int values representing the 3rd dimension of a / multiple 3d-box(es)
Returns
-------
x : int
the largest possible tower height
"""
assert(len(d1) >= 1)
assert(len(d1) == len(d2) == len(d3))
max_dimension = max([
max(d1),
max(d2),
max(d3)
])
boxes = zip(d1, d2, d3)
boxes = map(sorted, boxes)
boxes = sorted(boxes, key=lambda e: e[2])
boxes = sorted(boxes, key=lambda e: e[1])
boxes = sorted(boxes, key=lambda e: e[0])
boxes = boxes + list(map(lambda e: [e[1], e[2], e[0]], boxes))
boxes = sorted(boxes, key=lambda e: e[2])
boxes = sorted(boxes, key=lambda e: e[1])
boxes = sorted(boxes, key=lambda e: e[0])
n = len(boxes)
# dimension 1: x-coordinate left
# dimension 2: y-coordinate left
max_height = [[0 for _ in range(max_dimension + 1)]
for _ in range(max_dimension + 1)]
for i in range(n):
box_x, box_y, box_z = boxes[i][0], boxes[i][1], boxes[i][2]
for x in range(box_x, max_dimension + 1):
for y in range(box_y, max_dimension + 1):
max_tmp = max_height[x][y]
if box_x <= x and box_y <= y:
max_tmp = max(
max_tmp, max_height[box_x-1][box_y-1] + box_z)
if box_x <= x and box_z <= y:
max_tmp = max(
max_tmp, max_height[box_x-1][box_z-1] + box_y)
if box_y <= x and box_z <= y:
max_tmp = max(
max_tmp, max_height[box_y-1][box_z-1] + box_x)
max_height[x][y] = max_tmp
return max_height[max_dimension][max_dimension]
def maxHeightTry3(d1, d2, d3):
"""
A method that calculates largest possible tower height of given boxes.
Problem description: https://practice.geeksforgeeks.org/problems/box-stacking/1
time complexity: O(n^2)
space complexity: O(n)
Parameters
----------
d1 : int[]
a list of int values representing the 1st dimension of a / multiple 3d-box(es)
d2 : int[]
a list of int values representing the 2nd dimension of a / multiple 3d-box(es)
d3 : int[]
a list of int values representing the 3rd dimension of a / multiple 3d-box(es)
Returns
-------
x : int
the largest possible tower height
"""
assert(len(d1) >= 1)
assert(len(d1) == len(d2) == len(d3))
boxes = zip(d1, d2, d3)
boxes = map(sorted, boxes)
boxes = sorted(boxes, key=lambda e: (e[0], e[1], e[2]))
boxes = boxes + list(map(lambda e: [e[1], e[2], e[0]], boxes))
boxes = boxes + list(map(lambda e: [e[0], e[2], e[1]], boxes))
boxes = sorted(boxes, key=lambda e: (e[0], e[1], e[2]))
n = len(boxes)
max_height = {(b[0], b[1]): b[2] for b in boxes}
for i in range(n):
box1_x, box1_y = boxes[i][0], boxes[i][1]
for j in range(i+1, n):
box2_x, box2_y, box2_z = boxes[j][0], boxes[j][1], boxes[j][2]
max_tmp = max_height[box2_x, box2_y]
if box1_x < box2_x and box1_y < box2_y:
max_tmp = max(max_tmp, max_height[box1_x, box1_y] + box2_z)
max_height[box2_x, box2_y] = max_tmp
return max([v for _, v in max_height.items()])
def maxHeight(d1, d2, d3):
"""
A method that calculates largest possible tower height of given boxes.
Problem description: https://practice.geeksforgeeks.org/problems/box-stacking/1
time complexity: O(n^2)
space complexity: O(n)
Parameters
----------
d1 : int[]
a list of int values representing the 1st dimension of a / multiple 3d-box(es)
d2 : int[]
a list of int values representing the 2nd dimension of a / multiple 3d-box(es)
d3 : int[]
a list of int values representing the 3rd dimension of a / multiple 3d-box(es)
Returns
-------
x : int
the largest possible tower height
"""
return maxHeightTry3(d1, d2, d3)