Skip to content

Commit 2e22a61

Browse files
committed
1691.py
1 parent b20534a commit 2e22a61

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

1600-1700/1691.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [widthi, lengthi, heighti] (0-indexed). Choose a subset of cuboids and place them on each other.
3+
4+
You can place cuboid i on cuboid j if widthi <= widthj and lengthi <= lengthj and heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid.
5+
6+
Return the maximum height of the stacked cuboids.
7+
8+
9+
Example 1:
10+
Input: cuboids = [[50,45,20],[95,37,53],[45,23,12]]
11+
Output: 190
12+
Explanation:
13+
Cuboid 1 is placed on the bottom with the 53x37 side facing down with height 95.
14+
Cuboid 0 is placed next with the 45x20 side facing down with height 50.
15+
Cuboid 2 is placed next with the 23x12 side facing down with height 45.
16+
The total height is 95 + 50 + 45 = 190.
17+
Example 2:
18+
19+
Input: cuboids = [[38,25,45],[76,35,3]]
20+
Output: 76
21+
Explanation:
22+
You can't place any of the cuboids on the other.
23+
We choose cuboid 1 and rotate it so that the 35x3 side is facing down and its height is 76.
24+
Example 3:
25+
26+
Input: cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
27+
Output: 102
28+
Explanation:
29+
After rearranging the cuboids, you can see that all cuboids have the same dimension.
30+
You can place the 11x7 side down on all cuboids so their heights are 17.
31+
The maximum height of stacked cuboids is 6 * 17 = 102.
32+
'''
33+
class Solution:
34+
def maxHeight(self, cuboids: List[List[int]]) -> int:
35+
N = len(cuboids)
36+
37+
for c in cuboids:
38+
c.sort()
39+
cuboids.sort()
40+
dp =[0] * N
41+
42+
for i in range(N):
43+
dp[i] = cuboids[i][2]
44+
for j in range(i):
45+
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
46+
dp[i] = max(dp[i], dp[j] + cuboids[i][2])
47+
48+
return max(dp)
49+
50+

0 commit comments

Comments
 (0)