Leetcode506.Relative Ranks相对名次
给出 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。所有运动员的成绩都不相同。
bool cmp1(int x, int y)
{
return x > y;
}
class Solution {
public:
int BS(vector<int> nums, int target)
{
int low = 0;
int high = nums.size() - 1;
while(low <= high)
{
int mid = (low + high) / 2;
if(nums[mid] == target)
{
return mid;
}
else if(nums[mid] < target)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return low;
}
vector<string> findRelativeRanks(vector<int>& nums) {
int len = nums.size();
vector<int> nums2 = nums;
sort(nums2.begin(), nums2.end(), cmp1);
vector<string> res;
for(int i = 0; i < len; i++)
{
if(nums[i] == nums2[0])
{
res.push_back("Gold Medal");
}
else if(nums[i] == nums2[1])
{
res.push_back("Silver Medal");
}
else if(nums[i] == nums2[2])
{
res.push_back("Bronze Medal");
}
else
{
string temp = "";
temp = temp + (to_string(BS(nums2, nums[i]) + 1));
res.push_back(temp);
}
}
return res;
}
};
Leetcode506.Relative Ranks相对名次的更多相关文章
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [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 Relative Ranks
原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
随机推荐
- mysql简单的操作
启动数据库服务 net start mysql 停止数据库服务 net stop mysql 退出数据库 exit 保存操作及结果 将在命令行窗口中 ...
- C# 获取今天是星期几
//获取今天是星期几 string[] Day = new string[] { "星期日", "星期一", "星期二", "星期 ...
- 锋利的Jquery(点击显示隐藏div)
点击显示隐藏div <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- grep 强大的文本搜索工具
1.grep -r "History folder does't exist:" * :中间是要搜索的文本,* 表示全部显示出来
- P4860 Roy&October之取石子II
4的倍数不行,之间的数都可以到4的倍数,而6的倍数不能到4的倍数 #include <iostream> #include <cstdio> #include <queu ...
- SpringBoot配置自定义日期参数转换器
1.自定义参数转换器 自定义参数转换器必须实现Converter接口 /** * Created by IntelliJ IDEA. * * @Auther: ShaoHsiung * @Date: ...
- 廖雪峰Java15JDBC编程-3JDBC接口-3JDBC更新
使用update语句的时候,需要通过JDBC实现update语句的执行,这个时候仍然通过PreparedStatement对象来使用,直接传入update语句,然后通过setObject传入占位符的值 ...
- Python-线程(2)
目录 GIL全局解释器锁 GIL 与 Lock 多进程 VS 多线程 死锁现象 递归锁 信号量 Semaphore 线程队列 GIL全局解释器锁 在Cpython解释器中,同一个进程下开启的多线程,同 ...
- nginx 遇见问题与解决问题
如果你的安装目录为/usr/local/nginx,那么nginx的错误日志目录就是/usr/local/nginx/logs/error.log. 2.如果error.log不存在 就进入 # vi ...
- LintCode_1 单例模式
从今天开始我的LintCode之旅,由于C/C++好久没有使用了,语法生疏不说,低级错误频繁出现,因此在做题之后,还会有部分时间复习语法项目. ---------------------------- ...