Queue Reconstruction by Height - Complete Solution Guide
Queue Reconstruction by Height is LeetCode problem 406, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i , k i ] represents the i th person of height h i with exactly k i other people in front who have a height greater than or equal to h i . 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 j , k j ] is the attributes of the j th person in the queue
Detailed Explanation
The problem asks us to reconstruct a queue of people given their height and the number of people in front of them with a height greater than or equal to their own. The input is an array `people`, where each element `people[i] = [h_i, k_i]` represents a person with height `h_i` and `k_i` people ahead of them who are at least as tall. The output should be the reconstructed queue as an array of `[height, k]` pairs.
Solution Approach
The provided solution uses a sorting-based approach followed by inserting each person into the result list (or array) at a specific index. First, the input array is sorted in descending order of height and, in case of a tie, in ascending order of the number of people ahead of them (k). After sorting, the algorithm iterates through the sorted array and inserts each person into the result array at the index specified by the person's 'k' value. This placement ensures that 'k' people taller or equal in height are in front of them in the constructed queue.
Step-by-Step Algorithm
- Step 1: Sort the input array `people`. The sorting criteria are first by height in descending order (taller people come first) and then by the number of people in front (k) in ascending order (lower k comes first for same height).
- Step 2: Create an empty result list `result` to store the reconstructed queue.
- Step 3: Iterate through the sorted `people` array. For each person `p = [h, k]`, insert them into the `result` list at index `k`.
- Step 4: After iterating through all people, the `result` list now contains the reconstructed queue. Convert the result list back to an array if required by the problem constraints.
Key Insights
- Insight 1: Sorting the people by height in descending order (and then by k in ascending order for same heights) is crucial. This allows us to insert people into the queue without affecting the counts of people already placed.
- Insight 2: Using an insertion-based approach, where we insert people at the index specified by their 'k' value, effectively places them in the correct position relative to taller or equally tall people.
- Insight 3: The insertion step is the core of the algorithm. It leverages the sorted order to maintain the 'k' constraint during the reconstruction process.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: Array, Binary Indexed Tree, Segment Tree, Sorting.
Companies
Asked at: PhonePe.