Ninja And Stack Of Boxes
Ninja has been given ‘N’ rectangular 3-D ‘BOXES’. Each box has dimensions Length ‘L’, Breadth ‘B’ and Height ‘H’. Ninja has to create a stack of boxes as tall as possible. But there is a condition to do so.
You can only place a box on top of another box only 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. You can rotate any box so that any side of the box is used as a base. You can take multiple instances of any box.
Can you help the Ninja to create a stack of boxes as tall as possible?.
Input Format :
The first line of input contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains a space-separated integer ‘N’, which represents the number of boxes.
The next ‘N’ lines of each test case contain three space-separated integers ‘L’, ‘B’, and ’H’ that represent the length, breadth, and height of a box.
Output Format :
For each test case, print the maximum height of the stack which can be made using these boxes.
Print the output of each test case in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘T’ <= 100
1 <= ‘N’ <= 5000
1 <= ‘L’, ‘B’, ‘H’ <= 100000
Time Limit: 1 second
CodingNinjas
author
2y
Recursive
As we know we can place a box upon another box if the base area is smaller than the previously selected box. So, first of all, we sort the boxes according to the base area. Then we traverse t...read more
CodingNinjas
author
2y
Memoization
We can also optimize our previous approach. There are a lot of overlapping subproblems so we can use an array/vector ‘MEMO’ where ‘MEMO[i]’ denotes the maximum height when the number of box...read more
CodingNinjas
author
2y
Dynamic Programming
As we know we can place a box upon another box if the base area is smaller than the previously selected box. So, first of all, we sort the boxes according to the base area. Then we ...read more
Add answer anonymously...
Top Nutanix Mts1 interview questions & answers
Popular interview questions of Mts1
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app