
Problem
Solution
Submissions
Queue Reconstruction by Height
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C program to reconstruct a queue based on people's heights and the number of people in front of them who are taller or equal in height. You are given an array of people where people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front of them who have a height greater than or equal to hi. Reconstruct and return the queue represented by such an array.
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: Person with height 5 and 0 people taller in front goes first. Person with height 7 and 0 people taller in front goes second. Person with height 5 and 2 people taller in front goes third. Continuing this process gives us the final queue arrangement.
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]]
- Explanation: Start with the tallest people first as they don't affect shorter people's counts. Insert each person at the position indicated by their k value. The final arrangement satisfies all height and count constraints.
Constraints
- 1 ≤ people.length ≤ 2000
- 0 ≤ hi ≤ 10^6
- 0 ≤ ki < people.length
- Time Complexity: O(n^2)
- Space Complexity: O(n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Sort the people array by height in descending order, and by k value in ascending order for same heights
- Create a result array to store the final queue arrangement
- For each person, insert them at the position indicated by their k value
- Use insertion technique to place each person at the correct position
- The key insight is that taller people don't affect the k values of shorter people