![]() |
VOOZH | about |
Given three arrays height[], width[], and length[] of size n, where height[i], width[i], and length[i] represent the dimensions of a box. The task is to create a stack of boxes that is as tall as possible, but we can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box.
Note:
Example:
Input: height[] = [4, 1, 4, 10], width[] = [6, 2, 5, 12], length[] = [7, 3, 6, 32]
Output: 60
Explanation: One way of placing the boxes is as follows in the bottom to top manner: (Denoting the boxes in (l, w, h) manner)
(12, 32, 10) (10, 12, 32) (6, 7, 4)
(5, 6, 4) (4, 5, 6) (2, 3, 1) (1, 2, 3)
Hence, the total height of this stack is 10 + 32 + 4 + 4 + 6 + 1 + 3 = 60.
No other combination of boxes produces a height greater than this.Input: height[] = [1, 4, 3], width[] = [2, 5, 4], length[] = [3, 6, 1]
Output: 15
Explanation: One way of placing the boxes is as follows in the bottom to top manner: (Denoting the boxes in (l, w, h) manner)
(5, 6, 4) (4, 5, 6) (3, 4, 1), (2, 3, 1)
(1, 2, 3).
Hence, the total height of this stack is 4 + 6 + 1 + 1 + 3 = 15
No other combination of boxes produces a height greater than this.
The Box Stacking problem is a variation of LIS problem. The main idea is to maximize the height of the stack by considering all possible orientations of the boxes and find the optimal stacking order. For each box, we generate all six possible rotations by treating each dimension as the height once, and the remaining two dimensions as the base dimensions (width and depth). By doing this, we account for all possible orientations and allow multiple instances of the same box type in different orientations.
We then sort these box rotations by their length and breath in descending order, ensuring that smaller boxes are only placed on larger ones. We compute the maximum stack height for each box as the base by iterating through all prior boxes and checking if they can be stacked. This approach ensures that we evaluate all valid stacking combinations efficiently and find the maximum height achievable.
Table of Content
The idea is to recursively compute the maximum stack height starting from each box in all its possible orientations.
For a given box i with a particular orientation, the recursive relation is based on two conditions:
- We check if the current box i can be placed on top of any previously considered box j, meaning the base of box i must be strictly smaller than the base of box j.
- We compute the maximum stack height by choosing the best possible prior box to place under box i.
The recurrence relation can be written as:
- maxHeight[i] = max(height[i] + maxHeight[j] for all boxes j where base of box i > base of box j)
Here, maxHeight[i] represents the maximum height of the stack starting with box i in its current orientation. The recurrence works by checking all possible box placements and taking the maximum value among them, ensuring that the box stack grows in height as much as possible.
The base case is that a box by itself contributes a stack height of height[i], i.e., maxHeight[i] = height[i], when no boxes can be placed under it.
60
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: Maximum height of box stack at index i, i.e., maxHeight(i), depends on the optimal solutions of the subproblems maxHeight(j) for all j > i and base of j is smaller than i. By comparing these optimal substructures, we can efficiently calculate the maximum height of box stack at index i.
2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times.
60
The idea is to fill the DP table from bottom to up. The table is filled in an iterative manner from i = n-1 to i = 0. For each box i, The dynamic programming relation is as follows:
- set dp[i] = height[i]
- For j > i and base of j is smaller than base of i, set dp[i] = max(dp[i], height[i] + dp[j]).
60