-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_2418_sortPeople.cc
50 lines (44 loc) · 1013 Bytes
/
Problem_2418_sortPeople.cc
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
49
50
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
vector<string> sortPeople(vector<string> &names, vector<int> &heights)
{
int n = names.size();
vector<int> idx(n);
for (int i = 0; i < n; i++)
{
idx[i] = i;
}
std::sort(idx.begin(), idx.end(), [&](int l, int r) { return heights[l] > heights[r]; });
vector<string> ans(n);
for (int i = 0; i < n; i++)
{
ans[i] = names[idx[i]];
}
return ans;
}
};
void testSortPeople()
{
Solution s;
vector<string> n1 = {"Mary", "John", "Emma"};
vector<int> h1 = {180, 165, 170};
vector<string> o1 = {"Mary", "Emma", "John"};
vector<string> n2 = {"Alice", "Bob", "Bob"};
vector<int> h2 = {155, 185, 150};
vector<string> o2 = {"Bob", "Alice", "Bob"};
EXPECT_TRUE(o1 == s.sortPeople(n1, h1));
EXPECT_TRUE(o2 == s.sortPeople(n2, h2));
EXPECT_SUMMARY;
}
int main()
{
testSortPeople();
return 0;
}