#406
Medium Algorithms Queue reconstruction by height
Array Binary Indexed Tree Segment Tree Sorting
74.6% acceptance
Jan 13, 2026
7242
753
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] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.
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] = [hj, kj] is the attributes of the jth person in the queue (queue[0] is the person at the front of the queue).
Solution
Rust
Time O(n log n)
Space O(n)
impl Solution {
pub fn reconstruct_queue(people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut people = people;
people.sort_by(|a, b| {
if a[0] != b[0] {
b[0].cmp(&a[0])
} else {
a[1].cmp(&b[1])
}
});
let mut result = Vec::new();
for person in people {
result.insert(person[1] as usize, person);
}
result
}
}