Maximum Height by Stacking Cuboids

Given n cuboids where the dimensions of the i^th cuboid are cuboids[i] = [widthi, lengthi, heighti] (0-indexed). Choose a subset of cuboids and place them on each other.

You can place cuboid i on cuboid j if widthi <= widthj, lengthi <= lengthj, and heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid.

Return the maximum height of the stacked cuboids.

Example 1
Inputcuboids = [[50,45,20],[95,37,53],[45,23,12]]
Output190
Cuboid 1 is placed on the bottom with height 95, cuboid 0 is placed next with height 50, and cuboid 2 is placed next with height 45, for a total height of 190.
Example 2
Inputcuboids = [[38,25,45],[76,35,3]]
Output76
You can't place either cuboid on the other, so choosing cuboid 1 and rotating it gives a maximum height of 76.

Constraints

  • n == cuboids.length
  • 1 <= n <= 100
  • 1 <= widthi, lengthi, heighti <= 100

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate