Reconstruct a queue from heights and counts of people at least as tall in front
Company: ByteDance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
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] = [h, k]` represents a person of height `h` with exactly `k` other people in front who have a height greater than or equal to `h`.
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] = [h, k]` is the attributes of the person at position `j` in the queue (`queue[0]` is the person at the front of the queue).
### Function Signature
```python
def reconstruct_queue(people: list[list[int]]) -> list[list[int]]:
```
### Rules
- Each `people[i]` is a two-element list `[h, k]`. Return every input pair exactly once, ordered from the front of the queue to the back.
- `k` counts the people in front whose height is greater than or equal to `h`, so a person of the same height in front counts.
- The input always describes a valid queue, and a valid input has exactly one reconstruction, so the expected output is unique.
### Constraints
- `1 <= people.length <= 2000`
- `0 <= h <= 10⁶`
- `0 <= k < people.length`
- It is guaranteed that the queue can be reconstructed.
### Examples
```text
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]]
```
Reading from the front, every count checks out. For example, `[5,2]` has `[5,0]` and `[7,0]` ahead of it, and `[4,4]` has four people ahead of it, all taller.
```text
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]]
```
`[6,0]` can stand last because no one ahead of it is as tall as 6. `[3,2]` has `[4,0]` and `[5,0]` ahead of it, while the shorter `[2,2]` ahead of it does not count.
```text
Input: people = [[5,2],[5,0],[5,1]]
Output: [[5,0],[5,1],[5,2]]
```
Equal heights count toward `k`, so people of the same height stand in increasing order of `k`.
Overview: Rebuild a queue from a shuffled list of people, where each person records their height and how many people at least as tall stand in front of them. It tests greedy reasoning, sorting with a tie-break rule, and proving that a step-by-step construction gives the only valid queue.
You are given a list `people` describing the people standing in a queue, listed in no particular order. Each entry `people[i] = [h, k]` describes one person: `h` is that person's height, and `k` is the exact number of other people standing in front of that person whose height is greater than or equal to `h`.
Reconstruct the queue and return it as a list `queue`, where `queue[j] = [h, k]` holds the attributes of the person at position `j`. `queue[0]` is the person at the front of the queue.
Implement `reconstruct_queue(people)`. The input is a list of two-element integer lists (`int[][]` in Java, `std::vector<std::vector<int>>` in C++), and the return value has the same shape.
Rules:
- Each `people[i]` is a two-element list `[h, k]`. Return every input pair exactly once, ordered from the front of the queue to the back.
- `k` counts the people in front whose height is greater than or equal to `h`, so a person of the same height standing in front counts toward `k`.
- The input always describes a valid queue, and a valid input has exactly one reconstruction, so the expected output is unique.
Constraints:
- `1 <= people.length <= 2000`
- `people[i].length == 2`
- `0 <= h <= 10^6`
- `0 <= k < people.length`
- It is guaranteed that the queue can be reconstructed.
- Every value fits in a 32-bit signed integer; nothing exceeds 2^31-1.
Example 1:
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]]
Reading from the front, every count checks out. For example, `[5,2]` has `[5,0]` and `[7,0]` ahead of it, and `[4,4]` has four people ahead of it, all taller.
Example 2:
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]]
`[6,0]` can stand last because no one ahead of it is as tall as 6. `[3,2]` has `[4,0]` and `[5,0]` ahead of it, while the shorter `[2,2]` ahead of it does not count.
Constraints
- 1 <= people.length <= 2000
- people[i].length == 2, written as [h, k]
- 0 <= h <= 10^6
- 0 <= k < people.length
- It is guaranteed that the queue can be reconstructed.
- Every value fits in a 32-bit signed integer.
Examples
Input: ([[42, 0]],)
Expected Output: [[42, 0]]
Explanation: Minimum valid input: a single person stands alone at the front.
Input: ([[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]],)
Expected Output: [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
Explanation: First worked example from the statement; people of height 5 and 7 interleave with others.
Hints
- A person's k depends only on the people in front who are at least as tall, so a shorter person can stand anywhere without changing it.
- People of the same height count each other, so among them the order follows k from smallest to largest.
- You can check a proposed queue by walking from the front and counting, for each person, how many people ahead are at least as tall.