Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Queue Reconstruction by Height in JavaScript
The Queue Reconstruction by Height problem involves arranging people in a queue based on their height and the number of taller people in front of them. Each person is represented as [h, k] where h is height and k is the count of people in front with height ? h.
Understanding the Problem
Given an array of people where each person is represented by [height, k], we need to reconstruct the queue. The key insight is that taller people don't affect the positioning of shorter people, so we can process people from tallest to shortest.
For example, if we have people [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]], the reconstructed queue should place people such that each person has exactly k people in front of them who are taller or equal in height.
Algorithm Approach
Step 1: Sort people by height (descending), then by k-value (ascending) for same heights.
Step 2: Create an empty result array.
Step 3: Insert each person at index specified by their k-value using array splice.
Step 4: Return the reconstructed queue.
Implementation
function reconstructQueue(people) {
// Sort by height (descending), then by k-value (ascending)
people.sort((a, b) => {
if (a[0] === b[0]) {
return a[1] - b[1]; // Same height: sort by k ascending
} else {
return b[0] - a[0]; // Different height: sort by height descending
}
});
let result = [];
// Insert each person at their k-value index
for (let i = 0; i < people.length; i++) {
let person = people[i];
let k = person[1];
result.splice(k, 0, person);
}
return result;
}
// Test the function
const people = [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]];
const reconstructed = reconstructQueue(people);
console.log("Original:", people);
console.log("Reconstructed:", reconstructed);
// Verify the result
console.log("\nVerification:");
for (let i = 0; i < reconstructed.length; i++) {
let [height, expectedCount] = reconstructed[i];
let actualCount = 0;
// Count people in front with height >= current height
for (let j = 0; j < i; j++) {
if (reconstructed[j][0] >= height) {
actualCount++;
}
}
console.log(`Person [${height},${expectedCount}]: Expected ${expectedCount}, Actual ${actualCount}`);
}
Original: [ [ 7, 0 ], [ 4, 4 ], [ 7, 1 ], [ 5, 0 ], [ 6, 1 ], [ 5, 2 ] ] Reconstructed: [ [ 5, 0 ], [ 7, 0 ], [ 5, 2 ], [ 6, 1 ], [ 4, 4 ], [ 7, 1 ] ] Verification: Person [5,0]: Expected 0, Actual 0 Person [7,0]: Expected 0, Actual 0 Person [5,2]: Expected 2, Actual 2 Person [6,1]: Expected 1, Actual 1 Person [4,4]: Expected 4, Actual 4 Person [7,1]: Expected 1, Actual 1
How It Works
The algorithm works by processing people in order of decreasing height. When we insert a person with k-value at index k, all people already in the result are taller or equal, so inserting at position k ensures exactly k taller people are in front.
Time and Space Complexity
| Operation | Time Complexity | Explanation |
|---|---|---|
| Sorting | O(n log n) | Standard comparison-based sort |
| Insertion Loop | O(n²) | n insertions, each splice takes O(n) |
| Overall Time | O(n²) | Dominated by insertion operations |
| Space | O(n) | Result array stores all people |
Conclusion
The Queue Reconstruction algorithm efficiently solves the problem by sorting people by height and using the greedy approach of inserting each person at their k-index. While the time complexity is O(n²) due to array splicing, this approach provides an intuitive and correct solution to the queue reconstruction problem.
