给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。
(注:分数越高的选手,排名越靠前。)
示例 1:
输入: [5, 4, 3, 2, 1]
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
提示:
    N 是一个正整数并且不会超过 10000。
    所有运动员的成绩都不相同。
详见:https://leetcode.com/problems/relative-ranks/description/

C++:

class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums)
{
int n = nums.size(), cnt = 1;
vector<string> res(n, "");
map<int, int> m;
for (int i = 0; i < n; ++i)
{
m[nums[i]] = i;
}
for (auto it = m.rbegin(); it != m.rend(); ++it)
{
if (cnt == 1)
{
res[it->second] = "Gold Medal";
}
else if (cnt == 2)
{
res[it->second] = "Silver Medal";
}
else if (cnt == 3)
{
res[it->second] = "Bronze Medal";
}
else
{
res[it->second] = to_string(cnt);
}
++cnt;
}
return res;
}
};

参考:http://www.cnblogs.com/grandyang/p/6476983.html

506 Relative Ranks 相对名次的更多相关文章

  1. 【leetcode】506. Relative Ranks

    problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...

  2. 506. Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  3. [LeetCode&Python] Problem 506. Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  4. 【LeetCode】506. Relative Ranks 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...

  5. Leetcode506.Relative Ranks相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌"(" ...

  6. LeetCode 506. 相对名次(Relative Ranks) 39

    506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...

  7. [Swift]LeetCode506. 相对名次 | Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  8. [LeetCode] Relative Ranks 相对排名

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  9. [LeetCode] 506. Relative Ranks_Easy tag: Sort

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

随机推荐

  1. Android实现RecyclerView的下拉刷新和上拉载入很多其它

    需求 先上效果图, Material Design风格的下拉刷新和上拉载入很多其它. 源代码地址(欢迎star) https://github.com/studychen/SeeNewsV2 假设对于 ...

  2. 4.改变eclipse选中文字颜色

    window-preferences-general-editors-text editors-annotations-occurrences 和 window-preferences-general ...

  3. MessageBox_ swt

    SWT有不同类型的对话框.有些对话框具有特殊的属性. MessageBox messageBox = new MessageBox(shell, SWT.OK|SWT.CANCEL); if (mes ...

  4. 百度自然语言处理api用法

    def words url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?access_token=1111111" param ...

  5. 解决ubuntu10.04不能上网

    1:命令行输入:lspci查看驱动,最后几行如果有ethernet controller:atheros communications ar8151 v1.0*的话,就说明驱动没有安装好, 2:下载地 ...

  6. POJ2253 Frogger —— 最短路变形

    题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  7. asp.net cookie and session

    客户端只保存session id,信息存在服务端 Session状态应该存储在两个地方,分别是客户端和服务器端. 客户端只负责保存相应网站的SessionID,而其他的Session信息则保存在服务器 ...

  8. Scrapy运行报错:ModuleNotFoundError: No module named 'douban.douban'

    运行scrapy爬虫报错: from douban.douban.items import DoubanItem ModuleNotFoundError: No module named 'douba ...

  9. Laravel中常见的错误与解决方法小结

    一.报错: 「Can't swap PDO instance while within transaction」 通过查询 Laravel 源代码,可以确认异常是在 setPdo 方法中抛出的: ? ...

  10. BZOJ1787 meet

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1787 水题,但是结论很有趣. 题目求的是距离三个点之和最小的点. 这个很显然是在三个LCA上, ...