Quick 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.

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

  1. 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.
  2. People of the same height count each other, so among them the order follows k from smallest to largest.
  3. 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.

Reconstruct a Queue from Heights and Counts Ahead

mediumByteDanceSoftware Engineer

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.

Examples

Example 1
Input
([[42, 0]],)
Output
[[42, 0]]
Notes
Minimum valid input: a single person stands alone at the front.
Example 2
Input
([[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]]
Notes
First worked example from the statement; people of height 5 and 7 interleave with others.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def reconstruct_queue(people: list[list[int]]) -> list[list[int]]:
Sample call
reconstruct_queue([[42, 0]])
Language
Case 1
Input
([[42, 0]],)
Expected
[[42, 0]]
Case 2
Input
([[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]],)
Expected
[[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]

Browse More Questions

Show the approach

Approach

Sort the people by height from tallest to shortest, and among equal heights by k from smallest to largest. Then build the queue by inserting each person, in that order, at index k of the list built so far.

This works because, when a person [h, k] is inserted, everyone already in the list is at least as tall (taller, or the same height with a smaller k), so standing at index k gives exactly k people in front who are at least as tall. The people who must stand in front of [h, k] and count toward k are taller people and same-height people with a smaller k; all of them are already placed, so a valid input always has at least k people in the list and index k exists. Later insertions never change this count. A shorter person does not count toward k wherever it lands. A same-height person with a larger k' is inserted at index k', and at that moment this person still has exactly k people in front (only people at least as tall are in the list), so it sits at index k < k' and the newcomer lands behind it. Every count is therefore satisfied, and since a valid input has exactly one reconstruction, the result is the expected queue.

Edge cases: a single person comes back unchanged; when everyone has the same height, the queue is the people sorted by k; height 0 behaves like any other height. Values stay below 2^31-1, so 32-bit integers suffice in every language.

Time complexity:
O(n^2)
Space complexity:
O(n)