-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox_stacking_problem.py
More file actions
40 lines (32 loc) · 1.3 KB
/
Copy pathbox_stacking_problem.py
File metadata and controls
40 lines (32 loc) · 1.3 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
class Solution:
def boxStackingMaxHeight(self, boxes) -> int:
n = len(boxes)
all_boxes = [[0, 0, 0] for _ in range(n*3)]
index = 0
for i in range(n):
all_boxes[index] = [boxes[i][0], max(
boxes[i][1], boxes[i][2]), min(boxes[i][1], boxes[i][2])]
index += 1
all_boxes[index] = [boxes[i][1], max(
boxes[i][0], boxes[i][2]), min(boxes[i][0], boxes[i][2])]
index += 1
all_boxes[index] = [boxes[i][2], max(
boxes[i][0], boxes[i][1]), min(boxes[i][0], boxes[i][1])]
index += 1
n *= 3
all_boxes = sorted(all_boxes, key=lambda x: x[1]*x[2], reverse=True)
for i in range(len(all_boxes)):
print(all_boxes[i])
dp = [0 for _ in range(n)]
for i in range(n):
dp[i] = all_boxes[i][0]
for i in range(1, n):
for j in range(i):
if all_boxes[i][1] < all_boxes[j][1] and all_boxes[i][2] < all_boxes[j][2]:
if dp[i] < dp[j] + all_boxes[i][0]:
dp[i] = dp[j] + all_boxes[i][0]
return max(dp)
if __name__ == '__main__':
boxes = [[4, 6, 7], [1, 2, 3], [4, 5, 6], [10, 12, 32]]
sol = Solution()
print(sol.boxStackingMaxHeight(boxes))