15% off all plans & roadmaps Use code
See plansLeetCode 406 · Solution & editorial
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] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.
Queue Reconstruction by Height is a Greedy Patterns problem, in the Sorting Based subpattern. The pattern covers interval scheduling, jump games, stock trading, and task scheduling
Solve greedy problems that require sorting as preprocessing step for optimal solutions.
The reference solution runs in O(n * sqrt(n)) time and O(n) space.
Queue Reconstruction by Height has been reported in interviews at Google, Facebook.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Queue Reconstruction by Height is a Greedy Patterns problem, in the Sorting Based subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.