2418. Sort the People
You are given an array of strings names
, and an array heights
that consists of distinct positive integers. Both arrays are of length n
.
For each index i
, names[i]
and heights[i]
denote the name and height of the ith
person.
Return names
sorted in descending order by the people’s heights.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: vector<string> sortPeople(vector<string>& names, vector<int>& heights) { vector<pair<int,string>> S; for(int i = 0; i < names.size(); i++) { S.push_back({heights[i], names[i]}); } sort(begin(S), end(S)); vector<string> res; for(int i = S.size() - 1; i >= 0; i--) res.push_back(S[i].second); return res; } };
|