Maximum Height by Stacking Cuboids - Problem
Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [width_i, length_i, height_i] (0-indexed).
Choose a subset of cuboids and place them on each other. You can place cuboid i on cuboid j if width_i <= width_j and length_i <= length_j and height_i <= height_j.
You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid.
Return the maximum height of the stacked cuboids.
Input & Output
Example 1 — Basic Stacking
$
Input:
cuboids = [[50,45,20],[95,37,53],[45,23,12]]
›
Output:
190
💡 Note:
After sorting dimensions: [[20,45,50],[37,53,95],[12,23,45]]. Sorted cuboids: [[12,23,45],[20,45,50],[37,53,95]]. We can stack all three: 45+50+95 = 190.
Example 2 — Subset Selection
$
Input:
cuboids = [[38,25,45],[76,35,3]]
›
Output:
76
💡 Note:
After sorting: [[25,38,45],[3,35,76]]. We cannot stack them (25≤3 is false), so best is single cuboid with height 76.
Example 3 — Single Cuboid
$
Input:
cuboids = [[7,11,17]]
›
Output:
17
💡 Note:
Only one cuboid, maximum height is its largest dimension after rotation: max(7,11,17) = 17.
Constraints
- n == cuboids.length
- 1 ≤ n ≤ 100
- 1 ≤ widthi, lengthi, heighti ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
Cuboids with 3 dimensions each, can be rotated
2
Process
Sort and find optimal stacking arrangement
3
Output
Maximum achievable height of stacked cuboids
Key Takeaway
🎯 Key Insight: Sort cuboids by dimensions to transform into longest increasing subsequence problem
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code