1691. Maximum Height by Stacking Cuboids
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.
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.
Return the maximum height of the stacked cuboids
.
Example 1:

Input: cuboids = [[50,45,20],[95,37,53],[45,23,12]]
Output: 190
Explanation:
Cuboid 1 is placed on the bottom with the 53x37 side facing down with height 95.
Cuboid 0 is placed next with the 45x20 side facing down with height 50.
Cuboid 2 is placed next with the 23x12 side facing down with height 45.
The total height is 95 + 50 + 45 = 190.
Example 2:
Input: cuboids = [[38,25,45],[76,35,3]]
Output: 76
Explanation:
You can't place any of the cuboids on the other.
We choose cuboid 1 and rotate it so that the 35x3 side is facing down and its height is 76.
Example 3:
Input: cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
Output: 102
Explanation:
After rearranging the cuboids, you can see that all cuboids have the same dimension.
You can place the 11x7 side down on all cuboids so their heights are 17.
The maximum height of stacked cuboids is 6 * 17 = 102.
Constraints:
n == cuboids.length
1 <= n <= 100
1 <= widthi, lengthi, heighti <= 100
Solution
Idea: If the edges of one cuboid are smaller than another, it can be placed on that cuboids. -> It will be a good idea to sort the edges first so that we can know if it can be placed onto another cuboid. Then we sort the cuboids as well. With the order, we can guarantee that upcoming cuboids is more likely can be placed correctly.
A cuboids i can be placed under a cuboids j, if and only if the smalli >= smalli && midi >= midj && largei >= largej.
So the comparison fun is
public int compare(int[] a, int[] b) {
if(a[0] != b[0]) return Integer.comapre(a[0], b[0]);
if(a[1] != b[1]) return Integer.comapre(a[1], b[1]);
return Integer.comapre(a[2], b[2]);
}
Then we consider any combination with "Taken" or "Not taken". and compute the max height in each case. Then return the max height among those cases.
After all, to reduce the time complexity, we can use memorization to do it.
class Solution {
int h = 0;
int[][] memo;
public int maxHeight(int[][] cuboids) {
int m = cuboids.length;
memo = new int[m+1][m+1];
for(int[] cuboid : cuboids){
Arrays.sort(cuboid);
}
Arrays.sort(cuboids, (a, b) -> compare(b, a));
for(int i = 0; i < cuboids.length; i++){
h = Math.max(h, helper(cuboids, i, -1));
}
return h;
}
public int helper(int[][] cub, int index, int prev) {
if(index >= cub.length) return 0;
if(memo[index][prev+1] != 0) return memo[index][prev+1];
if(prev == -1 || (cub[prev][0] >= cub[index][0] &&
cub[prev][1] >= cub[index][1] && cub[prev][2] >= cub[index][2])) {
int height = cub[index][2];
memo[index][prev+1] = Math.max(height + helper(cub, index+1, index), helper(cub, index+1, prev));
}else{
memo[index][prev+1] = helper(cub, index+1, prev);
}
return memo[index][prev+1];
}
public int compare(int[] a, int[] b) {
if (a[2] != b[2])
return a[2] - b[2];
if (a[1] != b[1])
return a[1] - b[1];
return a[0] - b[0];
}
}
Last updated
Was this helpful?