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".

Example 1:

Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal".
For the left two athletes, you just need to output their relative ranks according to their scores.

Note:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

这道题给了我们一组分数,让我们求相对排名,前三名分别是金银铜牌,后面的就是名次数,不是一道难题,我们可以利用堆来排序,建立一个优先队列,把分数和其坐标位置放入队列中,会自动按其分数高低排序,然后我们从顶端开始一个一个取出数据,由于保存了其在原数组的位置,我们可以直接将其存到结果res中正确的位置,用一个变量cnt来记录名词,前三名给奖牌,后面就是名次数,参见代码如下:

解法一:

class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums) {
int n = nums.size(), cnt = ;
vector<string> res(n, "");
priority_queue<pair<int, int>> q;
for (int i = ; i < n; ++i) {
q.push({nums[i], i});
}
for (int i = ; i < n; ++i) {
int idx = q.top().second; q.pop();
if (cnt == ) res[idx] = "Gold Medal";
else if (cnt == ) res[idx] = "Silver Medal";
else if (cnt == ) res[idx] = "Bronze Medal";
else res[idx] = to_string(cnt);
++cnt;
}
return res;
}
};

下面这种方法思路和上面一样,不过数据结构用的不同,这里利用map的自动排序的功能,不过map是升序排列的,所以我们遍历的时候就要从最后面开始遍历,最后一个是金牌,然后往前一次是银牌,铜牌,名次数等,参见代码如下:

解法二:

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

下面这种方法没用什么炫的数据结构,就是数组,建立一个坐标数组,不过排序的时候比较的不是坐标,而是该坐标位置上对应的数字,后面的处理方法和之前的并没有什么不同,参见代码如下:

解法三:

class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums) {
int n = nums.size();
vector<int> idx(n);
vector<string> res(n, "");
for (int i = ; i < n; ++i) idx[i] = i;
sort(idx.begin(), idx.end(), [&](int a, int b){return nums[a] > nums[b];});
for (int i = ; i < n; ++i) {
if (i == ) res[idx[i]] = "Gold Medal";
else if (i == ) res[idx[i]] = "Silver Medal";
else if (i == ) res[idx[i]] = "Bronze Medal";
else res[idx[i]] = to_string(i + );
}
return res;
}
};

参考资料:

https://discuss.leetcode.com/topic/77912/c-easy-to-understand

https://discuss.leetcode.com/topic/77876/easy-java-solution-sorting

https://discuss.leetcode.com/topic/78244/simple-c-solution-using-a-map

https://discuss.leetcode.com/topic/77869/simple-sorting-o-n-log-n-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Relative Ranks 相对排名的更多相关文章

  1. LeetCode Relative Ranks

    原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...

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

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

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

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

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

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

  5. 【leetcode】506. Relative Ranks

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

  6. 506. Relative Ranks

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

  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(Java实现)

    这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个 ...

  9. 506 Relative Ranks 相对名次

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

随机推荐

  1. [SDOI2011]染色

    [SDOI2011]染色 题目描述 输入输出格式 输出格式: 对于每个询问操作,输出一行答案. 解法 ps:这题本来是树剖的,但我用lct写的,以下是lct的写法,树剖会有所不同 我们考虑把不同色点的 ...

  2. 基于hi-nginx的web开发(python篇)——cookie和会话管理

    hi-nginx通过redis管理会话. 要开启管理,需要做三件事. 第一件开启userid: userid on; userid_name SESSIONID; userid_domain loca ...

  3. java通过数据库连接池链接oracle

    开发工具:Eclipse J2EE 3.6 运行环境:jdk1.6 部署环境:Tomcat7 数据库连接池用的是dbcp,网上download下来的三个jar包. 把数据库连接池包和jdbc的包放到t ...

  4. tornado httpserver

    # coding:utf-8 import tornado.web import tornado.ioloop import tornado.httpserver # 新引入httpserver模块 ...

  5. Flask 扩展 Mail

    安装 pip install flask-mail from flask import Flask from flask_mail import Mail, Message app = Flask(_ ...

  6. Python 列表嵌套多种实现方式

    #coding=utf-8 list=[] for i in range(1,101): list.append(i) # print(list) tempList=[] newList=[] whi ...

  7. Linux下的Shell编程(1)最简单的例子

    深入地了解和熟练地掌握Shell编程,是每一个Linux用户的必修 功课之一. 从第一行开始 我们可以使用任意一种文字编辑器编写shell脚本,它必须以如下行开始(必须放在文件的第一行): #!/bi ...

  8. 阿里云API网关(8)开发指南-SDK下载

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  9. Easyui Datagrid 修改显示行号列宽度

    EasyUI中Datagrid的第一列显示行号,可是如果数据量大的的时候,显示行号的那一列数据会显示不完全的. 可以通过修改Datagrid的样式来解决这个问题,在样式中加入下面这个样式,就可以自己修 ...

  10. 命名参数名(含*args , * *kw的区别)

    要限制关键字参数的名字,就可以用命名关键字参数 # coding=utf-8 # 命名关键字参数需要一个特殊分隔符*,*后面的参数被视为命名关键字参数.调用方式如下 def person(name, ...