Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Height by Stacking Cuboids

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1691" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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

Explanation

Here's the solution to the cuboid stacking problem:

  • Sort Dimensions: For each cuboid, sort its dimensions in ascending order to represent its canonical form. This ensures that regardless of the initial orientation, we treat rotations consistently.
  • Sort Cuboids: Sort the cuboids based on their dimensions (width, length, height) in ascending order. This arrangement facilitates the application of dynamic programming.
  • Longest Increasing Subsequence (LIS): Apply dynamic programming to find the longest increasing subsequence of cuboids based on the condition that a cuboid can be placed on top of another. The height of each cuboid in the LIS is added to the total height.

  • Runtime Complexity: O(n2), Storage Complexity: O(n)

Code

    def max_height(cuboids):
    """
    Calculates the maximum height of stacked cuboids.

    Args:
        cuboids: A list of cuboids, where each cuboid is a list of three integers
                 representing its width, length, and height.

    Returns:
        The maximum height of the stacked cuboids.
    """

    n = len(cuboids)

    # Sort dimensions of each cuboid to have a consistent representation
    for i in range(n):
        cuboids[i].sort()

    # Sort cuboids based on their dimensions
    cuboids.sort()

    # dp[i] stores the maximum height ending with cuboid i
    dp = [0] * n

    for i in range(n):
        dp[i] = cuboids[i][2]  # Initialize with the height of the cuboid itself
        for j in range(i):
            if (cuboids[i][0] >= cuboids[j][0] and
                cuboids[i][1] >= cuboids[j][1] and
                cuboids[i][2] >= cuboids[j][2]):
                dp[i] = max(dp[i], dp[j] + cuboids[i][2])

    return max(dp)

More from this blog

C

Chatmagic blog

2894 posts