


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.
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.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 100
1 <= ‘N’ <= 5000
1 <= ‘L’, ‘B’, ‘H’ <= 100000
Time Limit: 1 second
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 through all the boxes and assume that the ‘i’th’ box is placed at the bottom. Then we try to place all the remaining boxes on this ‘i’th’ box and calculate the maximum height of the stack.
ninjaAndStackOfBoxesHelper(‘BOXES’, ‘i’) function is explained below:
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 boxes is ‘i’. And in which we store our previous calculated result.
ninjaAndStackOfBoxesHelper(‘BOXES’, ‘i’, ‘MEMO’ ) function is explained below:
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 declare an array/vector ‘MAXI_HEIGHT’ in which we store the maximum height of the stack of boxes when the ‘i’ box is at the top of the stack of boxes. To find this we use the longest Increasing subsequence approach.