[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 ...
随机推荐
- Java面试总结(二)
前几天去了几家公司面试,果然,基本全部倒在二面上,无语啊...不过幸好,到最后拿到了环球易购的offer,打算就这么好好呆着了,学习学习,努力努力,下面讲讲这几天的面试吧. 先是恒大,一个组长面试,答 ...
- [Scala] 安装及环境配置(图文)
Window 上安装配置 1.Java(JDK)环境配置,详见 Java(JDK)环境 2.从 Scala 官网下载安装包:https://downloads.lightbend.com/scala/ ...
- MySQL_执行计划详细说明
1 简要说明 id 表格查询的顺序编号. 降序查看,id相同的从上到下查查看. id可以为null ,当table为( union ,m,n )类型的时候,id为null,这个时候,id的 ...
- 浅谈element-ui中的BEM范式实践
日常的工作中,我们无时无刻不在和样式打交道.没有样式的页面就如同一部电影,被人随意地在不同地方做了截取. BEM规范应该是对于我们现在前端组件开发中我觉得是最合适的一套范式了.所以,我在自己的日常工作 ...
- 『转载』从内存资源中加载C++程序集:CMemLoadDll
MemLoadDll.h #if !defined(Q_OS_LINUX) #pragma once typedef BOOL (__stdcall *ProcDllMain)(HINSTANCE, ...
- 2018上C语言程序设计(高级)作业- 第1次作业
未来两周学习内容 复习指针的定义和引用 指针的应用场景: 指针作为函数参数(角色互换) 指针作为函数的参数返回多个值 指针.数组和地址间的关系 使用指针进行数组操作 数组名(指针)作为函数参数(冒泡排 ...
- Beta 第二天
今天遇到的困难: 组员对github极度的不适应 Android Studio版本不一致项目难以打开运行 移植云端的时候,愚蠢的把所有项目开发环境全部搬上去.本身云的内存小,性能差,我们花费了太多时间 ...
- Beta No.1
一.今日任务 重新熟悉整体项目 对整个项目在未来的beta冲刺中进程有一个合理的规划 由于我们送出的是一个负责前端的成员,引入的也是一个负责前端工作的女生,(女生做起美工比起男生更加得心应手吧)所以我 ...
- C语言第八次作业
一.PTA实验作业 题目1:统计一行文本的单词个数 1.本题PTA提交列表 2.设计思路 // 一个非空格和一个空格代表一个单词 char str[1000]: 存放一行文本 定义 I,j=0:用作循 ...
- win7 Anaconda 安装 scrapy模块
之前用了很多方法,都安装不成功,今天终于成功了..说下方法.. anaconda的清华镜像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ ...