Skip to main content
Medium Greedy Medium frequency

Queue Reconstruction by Height

Open on LeetCode

Approach Summary

Sort by height descending (ties: k ascending). Insert each person at index k. Taller people already placed, so k is still valid.

How to Recognize This Pattern

  • Place tallest first — they don't affect taller-count for shorter people
  • Greedy insertion at k-th position

Complexity Analysis

Time Complexity

O(n²)

Space Complexity

O(n)

Tags

Array Greedy Sorting

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee