title: Puzzle: Find the order of n persons standing in a line (based on their heights)
body: <p>Saw this question on Careercup.com:</p>
  <p>Given heights of n persons standing in a line and a list of numbers corresponding to each person (p) that gives the number of persons who are taller than p and standing in front of p. For example,</p>
  <p>Heights:  5 3 2 6 1 4</p>
  <p>InFronts:0 1 2 0 3 2</p>
  <p>Means that the actual actual order is:   5 3 2 1 6 4</p>
  <p>The question gets the two lists of Heights and InFronts, and should generate the order standing in line.</p>
  <h3>My solution:</h3>
  <p>It could be solved by first sorting the list in descending order. Obviously, to sort, we need to define an object Person (with two attributes of Height and InFront) and then sort Persons based on their height. Then, I would use two stacks, a <strong>main</strong> stack and a <strong>temp</strong> one, to build up the order.</p>
  <p>Starting from the tallest, put it in the main stack. If the next person had an InFront value of greater than the person on top of the stack, that means the new person should be added before the person on top. Therefore, we need to pop persons from the <strong>main</strong> stack, insert the new person, and then return the persons popped out in the first step (back to the main stack from temp one). I would use a temp stack to keep the order of the popped out persons. But how many should be popped out? Since the list is sorted, we need to pop exactly the number of persons in front of the new person, i.e. corresponding InFront.</p>
  <p>I think this solution works. But the worst case order would be O(n^2) -- when putting a person in place needs popping out all previous ones.</p>
  <p>Is there any other solutions? possibly in O(n)?</p>