[leetcode]506. Relative Ranks
Analysis
讨厌的人总是那么讨厌!—— [ummmm~]
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.
简单的排名问题。
Implement
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums) {
vector<int> copy;
for(auto num:nums){
copy.push_back(num);
}
sort(copy.begin(), copy.end());
map<int, int> ranks;
for(int i=0; i<copy.size(); i++)
ranks[copy[i]] = i+1;
int len = nums.size();
vector<string> res;
for(auto num:nums){
if(ranks[num] == len)
res.push_back("Gold Medal");
else if(ranks[num] == len-1)
res.push_back("Silver Medal");
else if(ranks[num] == len-2)
res.push_back("Bronze Medal");
else
res.push_back(to_string(len+1-ranks[num]));
}
return res;
}
};