Get height for tallest possible stack of boxes
The code implements a greedy approach (to be exact, n greedy attempts): as soon as a box can be placed, it is placed. It does not necessarily give an optimal solution. For example, with the set of boxes ((height, width)):

    5, 5
    4, 1
    3, 4
    2, 3


the algorithm will only consider towers

    (5,5) (4,1)
    (4,1)
    (3,4) (2,3)
    (2,3)


but not an optimal tower

    (5,5), (3,4), (2,3)


You are right about the complexity of the algorithm, but I am afraid it is not relevant if the algorithm itself is not correct.