Queue Reconstruction by Height - Problem

You are given an array of people, people, which represents the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the i-th person of height hi with exactly ki other people in front who have a height greater than or equal to hi.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of the j-th person in the queue (queue[0] is the person at the front of the queue).

Input & Output

Example 1 — Basic Queue Reconstruction
$ Input: people = [[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]]
💡 Note: Person [5,0] has 0 people ≥ 5 in front (correct). Person [7,0] has 0 people ≥ 7 in front (correct). Person [5,2] has 2 people ≥ 5 in front: [5,0] and [7,0] (correct). And so on for all positions.
Example 2 — Simple Case
$ Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
💡 Note: Each person is placed to satisfy their k-constraint: [4,0] needs 0 people ≥4 in front, [5,0] needs 0 people ≥5 in front, etc.
Example 3 — Same Heights
$ Input: people = [[7,0],[7,1],[6,1]]
Output: [[7,0],[6,1],[7,1]]
💡 Note: [7,0] goes first with 0 people ≥7 in front. [6,1] goes next with 1 person ≥6 in front ([7,0]). [7,1] goes last with 1 person ≥7 in front ([7,0]).

Constraints

  • 1 ≤ people.length ≤ 2000
  • 0 ≤ hi ≤ 106
  • 0 ≤ ki < people.length
  • It is guaranteed that the queue can be reconstructed

Visualization

Tap to expand
Queue Reconstruction by Height INPUT people array: [h, k] pairs [7, 0] [4, 4] [7, 1] [5, 0] [6, 1] [5, 2] h = height k = people in front >= h Unsorted Queue 7 k=0 4 k=4 7 k=1 5 k=0 6 k=1 5 k=2 ALGORITHM STEPS 1 Sort by height DESC then by k ASC [7,0] [7,1] [6,1] [5,0] [5,2] [4,4] 2 Insert at index k Process each person 3 Build result Insert shifts elements right 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]... Insert [5,2] at 2: ...[5,2]... Insert [4,4] at 4: ...[4,4]... 4 Return queue O(n^2) time, O(n) space FINAL RESULT Reconstructed Queue [5,0] idx 0 [7,0] idx 1 [5,2] idx 2 [6,1] idx 3 [4,4] idx 4 [7,1] idx 5 Verification: [5,0]: 0 people >= 5 ahead - OK [7,0]: 0 people >= 7 ahead - OK [5,2]: 2 (7,7) >= 5 ahead - OK [6,1]: 1 (7) >= 6 ahead - OK [4,4]: 4 (5,7,5,6) >= 4 - OK [7,1]: 1 (7) >= 7 ahead - OK All Valid! Key Insight: Greedy approach: Sort tallest first (by height DESC, then k ASC). When inserting tall people first, shorter people inserted later don't affect their k-values since shorter people don't count for taller ones. The k-value directly tells us the insertion index because all previously placed people are taller or equal. TutorialsPoint - Queue Reconstruction by Height | Optimal Greedy Solution
Asked in
Google 25 Amazon 18 Facebook 12 Microsoft 10
217.0K Views
Medium Frequency
~25 min Avg. Time
6.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen