Skip to content

Queue Reconstruction by Height

MediumGreedy
Decode it first. Clues, constraints, pattern and plan before any code.

People stood in a queue, but the list got shuffled. Each entry people[i] = [h, k] says the person has height h and that exactly k people in front of them are at least as tall as they are. Rebuild the queue and return it, front first.

people = [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]]
→ [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]

Constraints

  • 1 <= people.length <= 2000
  • 0 <= h <= 10^6
  • 0 <= k < people.length
  • The input always describes a valid queue.