Given a set of rectangular 3D boxes (cuboids), create a stack of boxes as tall as possible and return the maximum height of the stacked boxes.

A box can be placed on top of another box only if the dimensions of the 2D base of the lower box is each “strictly” larger than of the 2D base of the higher box. Note that “multiple” instances of the same box can be used, such that a box can be rotated to use any of its sides as the base, and the solution does not have to include the every box to achieve maximum height.

Consider the following boxes where each box has dimensions L × W × H:

(4 × 2 × 5)
(3 × 1 × 6)
(3 × 2 × 1)
(6 × 3 × 8)

The valid rotations (length more than the width) of the boxes are:

(4 × 2 × 5), (5 × 4 × 2), (5 × 2 × 4)
(3 × 1 × 6), (6 × 3 × 1), (6 × 1 × 3)
(3 × 2 × 1), (3 × 1 × 2), (2 × 1 × 3)
(6 × 3 × 8), (8 × 6 × 3), (8 × 3 × 6)

The maximum height possible is 22, which can be obtained by arranging the boxes in the following order:

(3 × 1 × 6)
(4 × 2 × 5)
(6 × 3 × 8)
(8 × 6 × 3)

Note that (3 × 2 × 1) box is not included to achieve the maximum height.

Practice this problem

The idea is to use dynamic programming to solve this problem. We start by generating all rotations of each box. For simplicity, we can easily enforce the constraint that a box’s width is never more than the length. After generating all rotations, sort the boxes in descending order of area and then apply the LIS algorithm to get the maximum height. Let L(i) store the maximum possible height when the i'th box is on the top. Then the recurrence is:

L(i) = height(i) + max(L(j) | j < i and block i can be put on top of block j)

Finally, the maximum height is the maximum value in L[]. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The maximum height is 22

Java


Download  Run Code

Output:

The maximum height is 22

Python


Download  Run Code

Output:

The maximum height is 22

The time complexity of the above solution is O(n2) and requires O(n) extra space, where n is the total number of boxes.

 
Problem source: https://people.computing.clemson.edu/~bcdean/dp_practice/