-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0506. Relative Ranks.cpp
More file actions
56 lines (48 loc) · 1.17 KB
/
0506. Relative Ranks.cpp
File metadata and controls
56 lines (48 loc) · 1.17 KB
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
51
52
53
54
55
56
// Task : https://leetcode.com/problems/relative-ranks/description/?envType=daily-question&envId=2024-05-08
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
std::vector<std::string> findRelativeRanks(std::vector<int>& score) {
std::vector<int> vec = score;
int size = vec.size();
std::vector<std::string> ranks;
std::map<int, int> mp;
sort(vec.begin(), vec.end(), std::greater<int>());
for (int i = 0; i < size; i++) {
mp[vec[i]] = i + 1;
}
for (int i : score) {
if (mp.find(i) != mp.end()) {
if (mp[i] == 1) {
ranks.push_back("Gold Medal");
}
else if (mp[i] == 2) {
ranks.push_back("Silver Medal");
}
else if (mp[i] == 3) {
ranks.push_back("Bronze Medal");
}
else {
ranks.push_back(std::to_string(mp[i]));
}
}
}
return ranks;
}
void printVec(std::vector<std::string> vec) {
for (size_t i = 0; i < vec.size(); i++) {
std::cout << vec[i] << " ";
}
std::cout << '\n';
}
int main() {
// Example 1:
std::vector<int> vec1 = { 5, 4, 3, 2, 1 };
printVec(findRelativeRanks(vec1));
// Example 2:
std::vector<int> vec2 = { 10, 3, 8, 9, 4 };
printVec(findRelativeRanks(vec2));
return 0;
}