Queue Reconstruction by Height

You are given an array people where people[i] = [h, k] represents a person with height h and k people in front of them who have a height greater than or equal to h. Reconstruct and return the queue that satisfies all people. Pattern focus: Sorting First. Sort by height descending and k ascending, then insert each person at index k to preserve the required prefix condition.

Input Format

people = list of [height, k] pairs

Output Format

reconstructed queue

Constraints

  • 1 <= people.length <= 10^5
  • 1 <= h <= 10^9
  • 0 <= k < people.length

Examples

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]]

Explanation:

This is the classic example: insert people in descending height order.

Example 2:

Input:

people = [[6,0],[5,0],[4,0],[3,0]]

Output:

[[3,0],[4,0],[5,0],[6,0]]

Explanation:

All k values are 0, so the shortest person ends up first after stable insertion.

Example 3:

Input:

people = [[5,0],[5,1],[5,2]]

Output:

[[5,0],[5,1],[5,2]]

Explanation:

With equal heights, ordering by k gives the unique valid queue.

Loading...
Queue Reconstruction by Height - Greedy