Greedy Algorithms8 sections · 316 units
Open in Course

Queue Reconstruction - Greedy Insight

Tallest first

Insert tallest people first.

When you insert a tall person, shorter people not yet inserted do not affect their kk value. Sort by height descending, then by kk ascending. Insert each person at index kk in the result. Why this works: when you insert person (h,k)(h, k), all people already in the result are at least as tall. Inserting at position kk means exactly kk people in front are taller or equal. Shorter people inserted later slot in without disrupting this.