The Box Stacking Statement: Given n rectangle boxes, that the i box has height h[i], width w[i] and depth d[i]. Create a stack of boxes that is the tallest one possible, but only can 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.
But in this problem, all the boxes are has the same height (h[1]=h[2]=...h[n]) and N <= 100000. And we don't need to rotate the boxes anymore.
Example:
n = 5
w[]={1,3,5,7,9}; d[]={5,7,4,6,8}The answer is: 3 ( [1x5] ; [3x7] ; [9x8] )
I only can solve it in O(n^2) but I need less than it (can be O(n) or O(nlogn)).
You can't solve it with
O(n), because otherwise you would have an algorithm to sortnnumbers inO(n).But you could solve it with
O(n log n).w'sin ascending order. If you have twow'sthat are equals, sort in descending order by theird's.LISproblem in the list ofd's, and you will have you longest stack.Please note that there are several approaches for
LISas far as I know, theDPapproach isO(n^2), but there are aO(n log n)approach which you can find one implementation and explanation(which I used) on GFG.Here is my implementation of your problem with Kotlin: