Queue Reconstruction by Height
Master this topic with zero to advance depth.
Queue Reconstruction by Height
You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [h_i, k_i] represents the i-th person of height h_i who has exactly k_i other people in front of them with a height greater than or equal to h_i.
Reconstruct and return the queue that is represented by the input array people.
Visual Representation
Input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
Step 1: Sort by height DESC, then k index ASC.
[[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]
Step 2: Insert into list at index k.
Insert [7,0] at 0: [[7,0]]
Insert [7,1] at 1: [[7,0], [7,1]]
Insert [6,1] at 1: [[7,0], [6,1], [7,1]]
Insert [5,0] at 0: [[5,0], [7,0], [6,1], [7,1]]
...Examples
Result matches the height/k-index constraints.
Level I: Brute Force Simulation (Find Empty Slots)
Intuition
If we sort people by height ascending, we can process each person and find their relative position by counting 'empty slots' in the final array. Each empty slot is reserved for someone taller (or same height) than the current person.
Thought Process
- Sort by height ASC, then DESC for same height.
- Create an empty result array of size .
- For each person
[h, k]:- We need people in front of us who are .
- Find the -th empty slot in the result array and place the person there.
Detailed Dry Run
people = [[7,0], [4,4], [7,1], [5,0], [5,2], [6,1]] Sorted (H asc, K desc): [[4,4], [5,2], [5,0], [6,1], [7,1], [7,0]]
- [4,4]: Put in 5th empty slot (idx 4). res=[_ _ _ _ [4,4] _]
- [5,2]: Put in 3rd empty slot (idx 2). res=[_ _ [5,2] _ [4,4] _]
- [5,0]: Put in 1st empty slot (idx 0). res=[[5,0] _ [5,2] _ [4,4] _]
- [6,1]: Put in 2nd empty slot (idx 3). res=[[5,0] _ [5,2] [6,1] [4,4] _]
Level II: Optimal Greedy (Sort and Insert)
Intuition
Taller people don't care about shorter people behind them. If we process taller people first, their relative k index in the final queue will be exactly equal to their index in the list.
Thought Process
- Sort DESC by height, then ASC by
k. - Insert each person into a dynamic list at index
k. - Shorter people inserted later won't change the count of taller people in front of existing elements.
Pattern: Greedy Insertion
Detailed Dry Run
people = [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]
| Step | Person | Action | Queue |
|---|---|---|---|
| 1 | [7,0] | Insert at 0 | [[7,0]] |
| 2 | [7,1] | Insert at 1 | [[7,0], [7,1]] |
| 3 | [6,1] | Insert at 1 | [[7,0], [6,1], [7,1]] |
| 4 | [5,0] | Insert at 0 | [[5,0], [7,0], [6,1], [7,1]] |
| 5 | [5,2] | Insert at 2 | [[5,0], [7,0], [5,2], [6,1], [7,1]] |
| 6 | [4,4] | Insert at 4 | [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] |
Level III: Logarithmic Optimal (Binary Indexed Tree)
Intuition
Finding the -th empty slot in is the bottleneck in the simulation. We can speed this up to using a Binary Indexed Tree (BIT) or Segment Tree by tracking the number of available slots.
Thought Process
- Use the height ASC sort from Level I.
- Imagine an array where 1 means 'empty' and 0 means 'filled'.
- We want a position where .
- We can find this using Binary Search over the BIT in or walking the Segment Tree in .
Detailed Dry Run
Sorted: [[4,4], [5,2], [5,0], [6,1], [7,1], [7,0]] BIT tracks empty slots (initially all 1s).
- [4,4]: Find k=4. BIT query/search gives pos 5. res[4]=[4,4]. BIT update(5, -1).
- [5,2]: Find k=2. BIT query search gives pos 3. res[2]=[5,2]. BIT update(3, -1).
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.