- sort the array by the height in descending order and if heights are the same then sort by the k number in ascending order.
- create an empty array and insert into kth position the sorted people one by one.
1: class Solution {
2: public:
3: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
4: auto comp = [](pair<int, int> &p1, pair<int, int> &p2) {
5: return (p1.first > p2.first) || (p1.first == p2.first && p1.second < p2.second);
6: };
7: sort(people.begin(), people.end(), comp);
8: vector<pair<int, int>> res;
9: for (auto p : people) {
10: res.insert(res.begin() + p.second, p);
11: }
12: return res;
13: }
14: };
No comments:
Post a Comment