Monday, July 17, 2017

Queue Reconstruction by Height
Come across this interesting coding example and thought of sharing my experience with this simple solution. 
Basically you have a rectangle array of 2 columns h represents person height and n represents number of tall people who have a height greater than or equal to h.. We need to reconstruct the queue.
Example
Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

My simple solution would be as follow
1. convert array to list of objects (Person class)
2. sort the list by number of tall ahead then by height in ascending order.
3. Go through the list to check/change objects to the correct position of the list.
4. convert the list back to an array


No comments:

Post a Comment