Box Stacking (GeeksforGeeks)
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.
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow . Each test case contains an integer N denoting the total no of boxes available. In the next line are 3*N space separated values denoting the height width and length of the N boxes.
Output:
For each test case in a new line output will be the highest possible stack height which could be formed.
Constraints:
1<=T<=100
1<=N<=100
1<=l,w,h<=100
Example (To be used for expected output) :
Input:
2
4
4 6 7 1 2 3 4 5 6 10 12 32
3
1 2 3 4 5 6 3 4 1
Output
60
15
Solution :
#include <iostream>
using namespace std;
#define MAX(a, b) a > b ? a : b;
#define MIN(a, b) a < b ? a : b;
class Box
{
public:
int w, h, l;
void Print()
{
cout << h << " " <<w << " " << l << endl;
}
Box(){}
Box(int h1, int w1, int l1)
{
h = h1;
l = MAX(l1, w1);
w = MIN(w1, l1);
}
};
int compare(const void *a, const void *b)
{
Box *a1 = (Box*)a;
Box *b1 = (Box*)b;
return (((b1)->w * (b1)->l) - ((a1)->l * (a1)->w) );
}
int maxHeight(int height[], int width[], int length[], int n)
{
Box *boxes = new Box[3 *n];
int idx = 0;
for (int i = 0; i < n; i++)
{
boxes[idx] = Box(height[i], width[i], length[i]);
idx++;
boxes[idx] = Box(width[i], height[i], length[i]);
idx++;
boxes[idx] = Box(length[i], width[i], height[i]);
idx++;
}
n = 3 * n;
qsort(boxes, n, sizeof(boxes[0]), compare);
int *temp = new int[n];
for (int i = 0; i < n; i++)
{
temp[i] = boxes[i].h;
}
int max = 0;
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (boxes[i].w < boxes[j].w && boxes[i].l < boxes[j].l)
{
if (temp[i] < boxes[i].h + temp[j])
{
temp[i] = boxes[i].h + temp[j];
if(max < temp[i] ) max = temp[i];
}
}
}
}
delete[]temp;
delete[]boxes;
return max;
}
Good Explanation :
https://www.youtube.com/watch?v=9mod_xRB-O0
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow . Each test case contains an integer N denoting the total no of boxes available. In the next line are 3*N space separated values denoting the height width and length of the N boxes.
Output:
For each test case in a new line output will be the highest possible stack height which could be formed.
Constraints:
1<=T<=100
1<=N<=100
1<=l,w,h<=100
Example (To be used for expected output) :
Input:
2
4
4 6 7 1 2 3 4 5 6 10 12 32
3
1 2 3 4 5 6 3 4 1
Output
60
15
Solution :
#include <iostream>
using namespace std;
#define MAX(a, b) a > b ? a : b;
#define MIN(a, b) a < b ? a : b;
class Box
{
public:
int w, h, l;
void Print()
{
cout << h << " " <<w << " " << l << endl;
}
Box(){}
Box(int h1, int w1, int l1)
{
h = h1;
l = MAX(l1, w1);
w = MIN(w1, l1);
}
};
int compare(const void *a, const void *b)
{
Box *a1 = (Box*)a;
Box *b1 = (Box*)b;
return (((b1)->w * (b1)->l) - ((a1)->l * (a1)->w) );
}
int maxHeight(int height[], int width[], int length[], int n)
{
Box *boxes = new Box[3 *n];
int idx = 0;
for (int i = 0; i < n; i++)
{
boxes[idx] = Box(height[i], width[i], length[i]);
idx++;
boxes[idx] = Box(width[i], height[i], length[i]);
idx++;
boxes[idx] = Box(length[i], width[i], height[i]);
idx++;
}
n = 3 * n;
qsort(boxes, n, sizeof(boxes[0]), compare);
int *temp = new int[n];
for (int i = 0; i < n; i++)
{
temp[i] = boxes[i].h;
}
int max = 0;
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (boxes[i].w < boxes[j].w && boxes[i].l < boxes[j].l)
{
if (temp[i] < boxes[i].h + temp[j])
{
temp[i] = boxes[i].h + temp[j];
if(max < temp[i] ) max = temp[i];
}
}
}
}
delete[]temp;
delete[]boxes;
return max;
}
Good Explanation :
https://www.youtube.com/watch?v=9mod_xRB-O0
Comments
Post a Comment