给出 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相对名次的更多相关文章

  1. 506 Relative Ranks 相对名次

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

  2. [Swift]LeetCode506. 相对名次 | Relative Ranks

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

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

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

  4. 506. Relative Ranks

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

  5. [LeetCode] Relative Ranks 相对排名

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

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

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

  7. LeetCode Relative Ranks

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

  8. 【leetcode】506. Relative Ranks

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

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

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

随机推荐

  1. 最近开始学习python,学习到了关于web的内容。

    然而在win10中IIS发布CGI脚本的时候遇到了各种各样的问题. 如ISAPI和CGI限制,权限限制等等,一一的百度解决了,最后又出现了 HTTP 错误 502.2 - Bad Gateway Th ...

  2. django汉化

    汉化admin后台管理站点 修改settings文件(将上表格内容改变为下表格): LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' LANGUAGE_CODE = ...

  3. PyInstaller打包Python源文件为可执行程序exe

    1. 安装PyInstaller 使用命令:pip install PyInstaller时可能会由于网络的问题出现以下问题: pip._vendor.urllib3.exceptions.ReadT ...

  4. 最全Linux常用命令大全

    查看系统系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMB ...

  5. virtualbox导入winXP系统OVA文件重启

    1,开启虚拟机 2,按f8进入安全模式,然后修改注册表: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Processor HKEY_LOC ...

  6. ListControl 设置表格行高与字体

    设置行高: CImageList   m_l;  m_l.Create(1,18,TRUE|ILC_COLOR32,1,0);   listCtrl.SetImageList(&m_l,LVS ...

  7. Android开发 获取View的尺寸的2个方法

    前言 总所周知,在activity启动的onCreate或者其他生命周期里去获取View的尺寸是错误的,因为很有可能View并没有初始化测量绘制完成.你这个时候获取的宽或的高不出意外就是0.所以,我们 ...

  8. LoadRunner参数化详解【转】

    距离上次使用loadrunner 已经有一年多的时间了.初做测试时在项目中用过,后面项目中用不到,自己把重点放在了工具之外的东西上,认为性能测试不仅仅是会用工具,最近又想有一把好的利器毕竟可以帮助自己 ...

  9. 五. Arrow Function 箭头函数

    箭头函数三大好处: 1. 简明的语法 举例: 如果只有一个参数,可以不加(),多个参数用 "," 隔开 2. 隐式返回 首先说下什么是显示返回,显示返回就是 return 加上你要 ...

  10. odoo 下 get_object_reference 函数

    get_object_reference是 ir.model.data 模块中下的一个函数 该函数通过调用ir.model.data 模块中另外一个函数 xmlid_lookup 返回结果 def g ...