• + 0 comments

    You can refine that by:

    1. Calculating the sum (height) of each stack
    2. Calculating the MIN of the stack heights
    3. For each stack: pop items as long as its height is greater than the minimum height (when poping, take care of updating the stack height)
    4. For each stack, when all excedentary elements are poped, check if the height is equal to the min height. If one stack is lower, go back to step 2. The algorithm is over when all stacks have the same height.

    There should be less "all stacks have the same height" checking :)