Advertisement

Maximum Height by Stacking Cuboids - LeetCode 1691 Solution

Maximum Height by Stacking Cuboids - Complete Solution Guide

Maximum Height by Stacking Cuboids is LeetCode problem 1691, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given n cuboids where the dimensions of the i th 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 . Example 1: Input: cuboids = [[50,45,20],[95,37,53],[45,23,12]] Output: 190 Ex

Detailed Explanation

The problem asks us to find the maximum possible height of a stack of cuboids, given a set of cuboids with varying widths, lengths, and heights. We can rearrange the dimensions of each cuboid (rotate it) to maximize its height when placed in the stack. A cuboid 'i' can be placed on top of cuboid 'j' only if the width, length, and height of 'i' are all less than or equal to the corresponding dimensions of 'j'. The goal is to select a subset of cuboids and stack them in a way that maximizes the overall height of the stack.

Solution Approach

The provided solution uses a dynamic programming approach to find the maximum stack height. First, it standardizes each cuboid by sorting its dimensions. Then, it sorts the cuboids based on their dimensions to establish a valid processing order. A dynamic programming array, `dp`, is used to store the maximum height of a stack ending with each cuboid. The solution iterates through the sorted cuboids, and for each cuboid `i`, it checks if any previous cuboid `j` can be placed beneath it. If it can, it updates `dp[i]` to be the maximum of its current value and the sum of `dp[j]` and the height of cuboid `i`. Finally, the solution returns the maximum value in the `dp` array.

Step-by-Step Algorithm

  1. Step 1: Sort the dimensions of each cuboid to standardize the representation (width <= length <= height).
  2. Step 2: Sort the cuboids based on their dimensions (e.g., width, then length, then height) to create a valid processing order.
  3. Step 3: Initialize a dynamic programming array `dp` of size `n` (number of cuboids), where `dp[i]` will store the maximum height of a stack ending with cuboid `i`.
  4. Step 4: Iterate through the sorted cuboids from `i = 0` to `n - 1`.
  5. Step 5: For each cuboid `i`, initialize `dp[i]` to its height (cuboids[i][2]).
  6. Step 6: Iterate through the previous cuboids from `j = 0` to `i - 1`.
  7. Step 7: For each previous cuboid `j`, check if cuboid `i` can be placed on top of cuboid `j` (i.e., cuboids[j][0] <= cuboids[i][0] and cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]).
  8. Step 8: If cuboid `i` can be placed on top of cuboid `j`, update `dp[i]` to be the maximum of its current value and `dp[j] + cuboids[i][2]`.
  9. Step 9: After iterating through all cuboids, find the maximum value in the `dp` array, which represents the maximum height of any stack of cuboids.

Key Insights

  • Insight 1: Sorting the dimensions of each cuboid is crucial. By always having the dimensions in ascending order, we standardize the representation and simplify the comparison logic later.
  • Insight 2: Sorting the cuboids themselves based on their dimensions (e.g., width, then length, then height) allows us to efficiently determine a valid stacking order using dynamic programming. We only need to check cuboids that come before the current one in the sorted order.
  • Insight 3: The problem can be solved using dynamic programming, where dp[i] represents the maximum height of a stack ending with the i-th cuboid. By iterating through the sorted cuboids and checking if a previous cuboid can be placed beneath the current one, we can update dp[i] accordingly.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array, Dynamic Programming, Sorting.

Companies

Asked at: Samsung.