406. Queue Reconstruction by Height Medium

@problem@discussion
#Array#Greedy#Binary Indexed Tree#Segment Tree#Sorting



1/**
2 * [406] Queue Reconstruction by Height
3 *
4 * 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 i^th person of height hi with exactly ki other people in front who have a height greater than or equal to hi.
5 * 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 j^th person in the queue (queue[0] is the person at the front of the queue).
6 *  
7 * Example 1:
8 * 
9 * Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
10 * Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
11 * Explanation:
12 * Person 0 has height 5 with no other people taller or the same height in front.
13 * Person 1 has height 7 with no other people taller or the same height in front.
14 * Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
15 * Person 3 has height 6 with one person taller or the same height in front, which is person 1.
16 * Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
17 * Person 5 has height 7 with one person taller or the same height in front, which is person 1.
18 * Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
19 * 
20 * Example 2:
21 * 
22 * Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
23 * Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	1 <= people.length <= 2000
29 * 	0 <= hi <= 10^6
30 * 	0 <= ki < people.length
31 * 	It is guaranteed that the queue can be reconstructed.
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/queue-reconstruction-by-height/
37// discuss: https://leetcode.com/problems/queue-reconstruction-by-height/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn reconstruct_queue(people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
43        vec![]
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_406() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.