1691. Maximum Height by Stacking Cuboids LeetCode Solution

In this guide, you will get 1691. Maximum Height by Stacking Cuboids LeetCode Solution with the best time and space complexity. The solution to Maximum Height by Stacking Cuboids problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Height by Stacking Cuboids solution in C++
  4. Maximum Height by Stacking Cuboids solution in Java
  5. Maximum Height by Stacking Cuboids solution in Python
  6. Additional Resources
1691. Maximum Height by Stacking Cuboids LeetCode Solution image

Problem Statement of 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 53×37 side facing down with height 95.
Cuboid 0 is placed next with the 45×20 side facing down with height 50.
Cuboid 2 is placed next with the 23×12 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 35×3 side is facing down and its height is 76.

See also  3090. Maximum Length Substring With Two Occurrences LeetCode Solution

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 11×7 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

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(n)

1691. Maximum Height by Stacking Cuboids LeetCode Solution in C++

class Solution {
 public:
  int maxHeight(vector<vector<int>>& cuboids) {
    // For each cuboid, sort it so that c[0] <= c[1] <= c[2].
    for (vector<int>& cuboid : cuboids)
      ranges::sort(cuboid);

    ranges::sort(cuboids);

    // dp[i] := the maximum height with cuboids[i] in the bottom
    vector<int> dp(cuboids.size());

    for (int i = 0; i < cuboids.size(); ++i)
      dp[i] = cuboids[i][2];

    for (int i = 1; i < cuboids.size(); ++i)
      for (int j = 0; j < i; ++j)
        if (cuboids[j][0] <= cuboids[i][0] &&  //
            cuboids[j][1] <= cuboids[i][1] &&  //
            cuboids[j][2] <= cuboids[i][2])
          dp[i] = max(dp[i], dp[j] + cuboids[i][2]);

    return ranges::max(dp);
  }
};
/* code provided by PROGIEZ */

1691. Maximum Height by Stacking Cuboids LeetCode Solution in Java

class Solution {
  public int maxHeight(int[][] cuboids) {
    // For each cuboid, sort it so that c[0] <= c[1] <= c[2].
    for (int[] cuboid : cuboids)
      Arrays.sort(cuboid);

    Arrays.sort(cuboids, new Comparator<int[]>() {
      @Override
      public int compare(int[] a, int[] b) {
        if (a[0] != b[0])
          return a[0] - b[0];
        if (a[1] != b[1])
          return a[1] - b[1];
        return a[2] - b[2];
      }
    });

    // dp[i] := the maximum height with cuboids[i] in the bottom
    int[] dp = new int[cuboids.length];

    for (int i = 0; i < cuboids.length; ++i)
      dp[i] = cuboids[i][2];

    for (int i = 1; i < cuboids.length; ++i)
      for (int j = 0; j < i; ++j)
        if (cuboids[j][0] <= cuboids[i][0] && //
            cuboids[j][1] <= cuboids[i][1] && //
            cuboids[j][2] <= cuboids[i][2])
          dp[i] = Math.max(dp[i], dp[j] + cuboids[i][2]);

    return Arrays.stream(dp).max().getAsInt();
  }
}
// code provided by PROGIEZ

1691. Maximum Height by Stacking Cuboids LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  3315. Construct the Minimum Bitwise Array II LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.