You are given a set of N types of rectangular 3-D boxes, where the ith box has height h, width w and length l. You task is to create a stack of boxes which is as tall as possible, but you 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. Of course, you can rotate a box so that any side functions as its base.It is also allowable to use multiple instances of the same type of box. You task is to complete the function maxHeight which returns the height of the highest possible stack so formed.
Note: Base of the lower box should be strictly larger than that of the new box we're going to place. This is in terms of both length and width, not just in terms of area. So, two boxes with same base cannot be placed one over the other.
Example 1:
Input:
n = 4
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.

Box Stacking Problem | DP-22 - GeeksforGeeks
https://www.youtube.com/watch?v=kLucR6-Q0GA&ab_channel=TECHDOSE
class Solution{
public:
/*The function takes an array of heights, width and
length as its 3 arguments where each index i value
determines the height, width, length of the ith box.
Here n is the total no of boxes.*/
struct box {
int l;
int w;
int h;
};
static bool compare(struct box b1, struct box b2) {
return b1.l*b1.w > b2.l*b2.w;
}
int maxHeight(int height[],int width[],int length[],int n)
{
int len = 3*n;
struct box b[len];
int idx = 0;
// Creating 3*n intances of boxes
for(int i=0;i<n;i++) {
b[idx].h = height[i];
b[idx].l = max(length[i],width[i]);
b[idx].w = min(length[i],width[i]);
idx++;
b[idx].h = width[i];
b[idx].l = max(length[i],height[i]);
b[idx].w = min(length[i],height[i]);
idx++;
b[idx].h = length[i];
b[idx].l = max(height[i],width[i]);
b[idx].w = min(height[i],width[i]);
idx++;
}
// Sorting boxes in terms of their base area in descending order
sort(b,b+len,compare);
vector<int> dp(len);
for(int i=0;i<len;i++)
dp[i] = b[i].h;
for(int i=1;i<len;i++)
{
for(int j=0;j<i;j++)
{
if(b[i].l<b[j].l and b[i].w<b[j].w and dp[i]<b[i].h+dp[j])
dp[i]=b[i].h+dp[j];
}
}
return *max_element(dp.begin(),dp.end());
}
};
Time Complexity: O(n^2)
Auxiliary Space: O(n)