给出 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. Phoenix(SQL On HBase)安装和使用报告

    一.为什么使用Phoenix二.安装Phoenix2.1 兼容问题?2.2 编译CDH版本的Phoenix2.3 安装Phoenix到CDH环境中三.Phoenix的使用3.1 phoenix的4种调 ...

  2. 集群环境搭建-SSH免密码登陆(二)

    1.打开sshd配置 命令: vi /etc/ssh/sshd_config 找到以下内容,并去掉注释符”#“ RSAAuthentication yes PubkeyAuthentication y ...

  3. 启动tomcat时,经常遇到的问题 8080 端口被占用

    启动tomcat失败时,弹出的窗口如上,说明8080被某个进程占用了 需要把占用8080端口的进程给kill掉 Win+R 输入运行cmd 打开命令提示符cmd.exe netstat -ano|fi ...

  4. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  5. I.MX6 2014 u-boot 测试修改

    /************************************************************************* * I.MX6 2014 u-boot 测试修改 ...

  6. JAVA Synchronized (三) volatile 与 synchronized 的比较

    一,volatile关键字的可见性 要想理解volatile关键字,得先了解下JAVA的内存模型,Java内存模型的抽象示意图如下: 从图中可以看出: ①每个线程都有一个自己的本地内存空间--线程栈空 ...

  7. 【转】Selenium模拟JQuery滑动解锁

    滑动解锁一直做UI自动化的难点之一,我补一篇滑动解锁的例子,希望能给初做Web UI自动化测试的同学一些思路. 首先先看个例子. https://www.helloweba.com/demo/2017 ...

  8. 转载:PowerPivot for excel 100 Create KPI

    PowerPivot for excel 100 Create KPI      最近在了解PowerPivot,遇到了一些问题,不过还好最近都解决了,下面介绍一下关于在PowerPivot里面如何创 ...

  9. E20180331-hm

    corresponding adj. 相当的,对应的; 通信的; 符合的,符合; 一致的; implicitly adv. 含蓄地; 暗示地; 无疑问地; 无保留地; causal  adj. 具有因 ...

  10. C++笔试题(十)

    这些题目相比其他公司的试题,较为基础,全部为C语言,没有涉及C++,但如果不细心,是很难得到较高分数的.另外大家转贴不要去掉我的个人信息啊.互相宣传下网站嘛.1. 找错 void test1() { ...