Skip to content

Commit 3830f74

Browse files
authored
Create sort-the-people.cpp
1 parent 02baf5b commit 3830f74

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

C++/sort-the-people.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
// sort
5+
class Solution {
6+
public:
7+
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
8+
vector<int> order(size(names));
9+
iota(begin(order), end(order), 0);
10+
sort(begin(order), end(order), [&](const auto& x, const auto& y) { return heights[x] > heights[y]; });
11+
vector<string> result(size(names));
12+
for (int i = 0; i < size(order); ++i) {
13+
result[i] = names[order[i]];
14+
}
15+
return result;
16+
}
17+
};

0 commit comments

Comments
 (0)