Coding question #4
Context
=======
Imagine there is a line of people waiting to buy tickets at a movie theater.
Someone pulls a fire alarm and the line has to scatter.
For each person from line we know the following information:
* How tall they are
* The number of people in front of them who were taller than them
Please return the line in its original order. You can assume you receive this
information as a set of people where a person is:
Example
=======
Person #1 has a height of 1, and they see 1 taller person in front of them
Person #2 has a height of 2, and they see 2 taller people in front of them
Person #3 has a height of 3, and they see 0 taller people in front of them
Person #4 has a height of 4, and they see 1 taller person in front of them
Person #5 has a height of 5, and they see 0 taller people in front of them
The original order was:
3 1 5 2 4
Lyee’s note:
If you’re shorter than the person in front of you, you can still see multiple
taller people in front of you. Say you’re 5’ tall and there’s a 6’ person in front
of you, and a 5.5’ person in front of them, you can still see 2 people? Why????
public class Person implementes Comparable{
public Integer height;
public Integer tallerPeopleInFront;
@Override
protected Boolean compareTo(Person person){
return this.height - person.height;
}
}
public ArrayList<Person> tallsort(ArrayList<Person>() people){
//Sort people by height using the compareTo method we wrote
people = people.sort();
Arraylist<Person> originalLine = new Arraylist<>();
//Now that the tallest person is first, we can determine the order trivially
people.stream.foreach(person -> {
if(originalLine.isEmpty())
originalLine.add(person);
for(i = 0; i < person.size(); ++)
{
if(person.tallerPeopleInFront + i == originalLine.size();
oringialLine.insert(i, person);
}
});
return originalLine;
}