medium +25 pts

Queue Reconstruction by Height

Rebuild a queue from (height, taller_count) pairs using a greedy height-first strategy.

You are given a list of people described by pairs `[h, k]`, where `h` is a person's height and `k` is the number of people in front of them who are **strictly taller** than them. The list is randomly shuffled. Reconstruct the correct queue order so that every pair's `k` matches the number of taller people standing ahead of that person. Implement the function `reconstruct_queue(people: list[list[int]]) -> list[list[int]]` that returns the original queue order as a list of pairs. If multiple valid orders exist, any valid order is accepted, but the test cases expect the specific order produced by the standard greedy algorithm (described below). **Algorithm guidance:** Sort people by height descending (tallest first), and for equal heights, by `k` ascending. Then insert each person into a result list at index equal to their `k` value. This is a standard greedy approach that produces a correct queue. For example, given `[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]`, the reconstructed queue is `[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]`. **Input constraints:** - `0 <= len(people) <= 1000` - Heights `h` are integers from 1 to 10^9. - `k` is an integer from 0 to len(people)-1. - It is guaranteed that at least one valid reconstruction exists. Return the reconstructed queue as a list of lists.

Constraints

0 ≤ n ≤ 1000; heights fit in a 32-bit integer; k is a valid index for a person's position in the queue.

Example

>>> reconstruct_queue([[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]])
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
25 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Sort people from tallest to shortest. For equal heights, sort by k ascending.
Insert each person into a result list at index equal to their k value.
The insertion works because taller people are already placed, so the current person's k directly gives their position among those taller or equal.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.