LeetCode Problem

How to Solve Queue Reconstruction by Height

To solve this problem, sort the people by their heights and use binary indexed trees to efficiently track positions. This problem tests knowledge of sorting and binary indexed trees. The goal is to reconstruct the queue in the correct order with efficient time complexity.

GhostInterview Help

Need help with Queue Reconstruction by Height without spending extra time grinding it?

GhostInterview can read Queue Reconstruction by Height from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #406Array plus Binary Indexed TreeReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array plus Binary Indexed Tree
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, sort the people by their heights and use binary indexed trees to efficiently track positions. This problem tests knowledge of sorting and binary indexed trees. The goal is to reconstruct the queue in the correct order with efficient time complexity.

Problem Statement

You are given an array of people, where each element represents a person with two values: height and the number of people in front who are taller or the same height. Your task is to reconstruct the queue such that each person’s position satisfies both their height and the number of taller people in front of them.

The input consists of an array where each person is represented by [height, k]. Your goal is to return the reconstructed queue as an array, ensuring that each person in the queue satisfies the condition based on their height and the number of people taller than or equal to them in front.

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

Person 0 has height 5 with no other people taller or the same height in front. Person 1 has height 7 with no other people taller or the same height in front. Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1. Person 3 has height 6 with one person taller or the same height in front, which is person 1. Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3. Person 5 has height 7 with one person taller or the same height in front, which is person 1. Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.

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

Example details omitted.

Constraints

  • 1 <= people.length <= 2000
  • 0 <= hi <= 106
  • 0 <= ki < people.length
  • It is guaranteed that the queue can be reconstructed.

Solution Approach

Sort by Height and Position

Start by sorting the people array by height in descending order. This ensures that the tallest people are considered first. For people of the same height, sort by the number of people in front (k) in ascending order to ensure that those with fewer people in front get placed first.

Insert into Queue Using Binary Indexed Tree

For each person in the sorted list, use a binary indexed tree (BIT) to track the available positions. This will help in efficiently finding the right position for each person based on the number of taller people already placed.

Reconstruct the Queue

Using the binary indexed tree, place each person in the correct position by checking the available spots. Update the BIT as you place each person to ensure subsequent people have access to correct available positions.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The sorting step takes O(n log n), where n is the length of the people array. The insertion process for each person takes O(log n) due to the binary indexed tree. Overall, the time complexity is O(n log n), with space complexity depending on the binary indexed tree used, which is O(n).

What Interviewers Usually Probe

  • Look for understanding of sorting and binary indexed trees.
  • Check if the candidate can explain the interaction between sorting and efficient placement using BIT.
  • Evaluate how the candidate optimizes for time complexity, particularly with larger inputs.

Common Pitfalls or Variants

Common pitfalls

  • Not understanding the need for sorting the array by height before inserting people into the queue.
  • Failing to use the binary indexed tree efficiently for tracking available positions.
  • Inserting people into incorrect positions due to not properly considering the 'k' value in the sorting step.

Follow-up variants

  • Changing the sorting order of people, such as sorting by height in ascending order first.
  • Optimizing the binary indexed tree for different constraints or array sizes.
  • Modifying the problem by including additional attributes for each person, such as age, to be taken into account during the reconstruction.

How GhostInterview Helps

  • GhostInterview provides solutions with step-by-step breakdowns, ensuring you understand how sorting and BIT interact in this problem.
  • With a deep dive into common pitfalls and interview signals, GhostInterview helps you improve your problem-solving approach.
  • Using our solutions, you'll be able to practice this problem with both brute-force and optimized approaches for the best understanding.

Topic Pages

FAQ

How do I efficiently reconstruct the queue by height?

Sort the people array by height in descending order, and then use a binary indexed tree to efficiently track available positions while placing people.

What is the role of the binary indexed tree in this problem?

The binary indexed tree (BIT) is used to track available positions in the queue and ensures efficient placement of each person based on the number of taller people already in front.

How does the sorting step help in reconstructing the queue?

Sorting the array by height first ensures that the tallest people are placed first, and the secondary sorting by the number of taller people in front guarantees the correct position for each person.

What is the time complexity of the solution?

The time complexity is O(n log n) due to the sorting and binary indexed tree operations, where n is the number of people in the input array.

Can the queue reconstruction be done without sorting?

No, sorting by height is crucial to ensure the correct order of placement, especially when handling multiple people of the same height.

GhostInterview Solver

Need direct help with Queue Reconstruction by Height instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Queue Reconstruction by Height from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.