-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort_the_people.cpp
48 lines (42 loc) · 1.05 KB
/
sort_the_people.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* https://leetcode.com/problems/sort-the-people/description/ */
/* STL */
class Solution {
public:
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
map<int,string>m;
for(int i=0;i<names.size();i++)
{
m[heights[i]]=names[i];
}
vector<string>ans;
// reverse(m.begin(),m.end());
for(auto i=m.rbegin();i!=m.rend();i++)
{
ans.push_back(i->second);
}
return ans;
}
};
/* using pair*/
class Solution {
public:
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
vector<pair<int,string>>v;
for(int i=0;i<names.size();i++)
{
v.push_back(make_pair(heights[i],names[i]));
}
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
vector<string> ans;
for(int i=0;i<names.size();i++)
{
ans.push_back(v[i].second);
}
for(auto i:v)
{
cout<<i.second<<" ";
}
return ans;
}
};