[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 ...
 
随机推荐
- 真是没想到,ikvm.net居然停止开发了。
			
看样子作者对.net已经失去了信心 http://weblog.ikvm.net/CommentView.aspx?guid=33ea525f-a291-418a-bd6a-abdf22d0662b# ...
 - 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)
			
好不容易又到周五了,周末终于可以休息休息了.写这一篇随笔只是心血来潮,下午问了一位朋友PAT考的如何,顺便看一下他考的试题,里面有最后一道题,是关于给出中序遍历和后序遍历然后求一个层次遍历.等等,我找 ...
 - 韩天峰博客 php基础知识学习记录
			
http://rango.swoole.com 写好PHP代码真的不容易,给大家几个建议: 慎用全局变量,全局变量不好管理的,会导致你的代码依赖于全局变量,而耦合度太高. 一定不要复制粘贴代码,可重用 ...
 - 有序的map LinkedHashMap
			
HashMap是无序的,HashMap在put的时候是根据key的hashcode进行hash然后放入对应的地方.所以在按照一定顺序put进HashMap中,然后遍历出HashMap的顺序跟put的顺 ...
 - Semaphore 源码分析
			
Semaphore 源码分析 1. 在阅读源码时做了大量的注释,并且做了一些测试分析源码内的执行流程,由于博客篇幅有限,并且代码阅读起来没有 IDE 方便,所以在 github 上提供JDK1.8 的 ...
 - Beta阶段敏捷冲刺报告-DAY3
			
Beta阶段敏捷冲刺报告-DAY3 Scrum Meeting 敏捷开发日期 2017.11.4 会议时间 12:30 会议地点 软工所 参会人员 全体成员 会议内容 当天任务确认,进度调整, 讨论时 ...
 - 团队作业6——展示博客(Alpha版本)
			
Deadline: 2017-12-3 23:00PM,以博客发表日期为准 评分基准 按时交 - 有分,检查的项目包括后文的两个方面 团队成员介绍 Alpha阶段进展 团队合作,各成员分工 Be ...
 - 项目Alpha冲刺Day10
			
一.会议照片 二.项目进展 1.今日安排 解决前后台联调问题,完善全局的请求和路由跳转处理,添加空文件完善路由信息,优化界面跳转等待.完成个人信息和修改密码.修改前台数据组织和方法调用方式.解决登录和 ...
 - Python strip()方法
			
描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除字符串头尾指定 ...
 - JAVA_SE基础——17.方法的重载
			
方法重载: 方法重载就是方法名称重复,加载参数不同. 具体规范: 一.方法名一定要相同. 二.方法的参数表必须不同,包括参数的类型或个数,以此区分不同的方法体. 1.如果参数个数不同,就不管它的参数类 ...