Skip to content

Commit 03d8934

Browse files
committed
good problem
1 parent 80072e4 commit 03d8934

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
3+
Suppose you have a random list of people standing in a queue. Each person is
4+
described by a pair of integers (h, k), where h is the height of the person and k is
5+
the number of people in front of this person who have a height greater than or equal to h.
6+
Write an algorithm to reconstruct the queue.
7+
8+
Note:
9+
The number of people is less than 1,100.
10+
11+
12+
Example
13+
14+
Input:
15+
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
16+
17+
Output:
18+
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
19+
20+
*/
21+
22+
//solution
23+
24+
class Solution {
25+
26+
public:
27+
static bool comparatorFunc(vector<int> a , vector<int> b)
28+
{
29+
//if same height then compare number of people in front
30+
if(a[0] == b[0]){
31+
return (a[1] > b[1]);
32+
}
33+
return (a[0] < b[0]);
34+
}
35+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people)
36+
{
37+
//sort in ascending order based on comparator
38+
sort(people.begin(),people.end(),comparatorFunc);
39+
40+
vector<vector<int>> result;
41+
42+
// starting from end
43+
for(int i=people.size()-1;i>=0;i--)
44+
{
45+
// note the use of insert here, we simply start inserting based on k value of each people
46+
result.insert(result.begin() + people[i][1], people[i]);
47+
}
48+
return result;
49+
}
50+
};

0 commit comments

Comments
 (0)