[LeetCode] Relative Ranks 相对排名
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:
- N is a positive integer and won't exceed 10,000.
- 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 相对排名的更多相关文章
- LeetCode Relative Ranks
原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode算法题-Relative Ranks(Java实现)
这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个 ...
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
随机推荐
- [poj2342]Anniversary party_树形dp
Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...
- 【Spring源码深度解析学习系列】核心类介绍(一)
一.DefaultListableBeanFactory 首先看一下结构 由图可知XmlBeanFactory继承自DefaultListableBeanFactory,而DefaultListabl ...
- RabbitMQ封装实战
先说下背景:上周开始给项目添加曾经没有过的消息中间件.虽然说,一路到头非常容易,直接google,万事不愁~可是生活远不仅是眼前的"苟且".首先是想使用其他项目使用过的一套对mq封 ...
- alpha冲刺第五天
一.合照 二.项目燃尽图 三.项目进展 调整了一些界面的布局 细化了部分小功能的界面 注册界面和服务器响应了,但是在insert数据库方面出现了错误 四.明日规划 继续研究如何将注册的内容插入数据库 ...
- Beta总结
45°炸 031502601 蔡鸿杰 031502604 陈甘霖 031502632 伍晨薇 一.写在Beta项目前 Beta 凡 事 预 则 立 二.GitHub传送门 Beta冲刺重要版本 三.用 ...
- Beta冲刺第二天
一.昨天的困难 没困难 二.今天进度 局部测试并修复出现的bug 1.林洋洋:修复登录页面显示问题,修复日程查询问题 2.黄腾达:修复创建协作开始时间和结束时间没做检验的问题 3.张合胜:修复页面内容 ...
- 20162323周楠《Java程序设计与数据结构》第五周总结
20162323周楠 2016-2017-2 <程序设计与数据结构>第五周学习总结 教材学习内容总结 1.面向对象软件设计的基本部分是确定程序中应该创建哪些类: 2.面向对象程序设计的核心 ...
- 详谈C++虚函数表那回事(一般继承关系)
沿途总是会出现关于C++虚函数表的问题,今天做一总结: 1.什么是虚函数表: 虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实现的.简称为V-Table. ...
- css3动画 一行字鼠标触发 hover 从左到右颜色渐变
偶然的机会发现的这个东东 这几天做公司的官网 老板突然说出了一个外国网站 我就顺手搜了 并没有发现他说的高科技 但是一个东西深深地吸引了我 就是我下面要说的动画 这个好像不能放视频 我就简单的描述一 ...
- xxe漏洞检测及代码执行过程
这两天看了xxe漏洞,写一下自己的理解,xxe漏洞主要针对webservice危险的引用的外部实体并且未对外部实体进行敏感字符的过滤,从而可以造成命令执行,目録遍历等.首先存在漏洞的web服务一定是存 ...