给出 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. echars前端处理数据、pyechars后端处理数据

    echars -- 后端给数据,前端根据数据做渲染 - echarts:https://www.echartsjs.com/zh/index.htmlhtml文件 <!DOCTYPE html& ...

  2. 面试系列16 redis的持久化

    1.RDB和AOF两种持久化机制的介绍 RDB持久化机制,对redis中的数据执行周期性的持久化 AOF机制对每条写入命令作为日志,以append-only的模式写入一个日志文件中,在redis重启的 ...

  3. C++自己用模板减少工作量

    今天写代码,遇到这样一个类似如下的需求: auto componentClassSavedByPLC = std::make_shared<ComponentClassSavedByPLC> ...

  4. 建立 CRAMFS 包

    下载 cramfs 工具 从 http://prdownloads.sourceforge.net/cramfs/cramfs­1.1.tar.gz下载源代码包. 把下载包拷贝到 dev_home/t ...

  5. 关于Windows10企业版的激活方法

    今天打开Excel在使用的时候,突然弹出弹窗,说我的激活即将过期什么的,让我转到设置进行激活. 第一个想到的办法就是更换产品密钥,在网上找了不少产品密钥,密钥有效,但是需要连接企业激活什么的,因为我是 ...

  6. 算法系列:Shell 排序

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  7. idea-----idea的项目中output框出现乱码

    找到tomcat的安装目录中的logging.properties 我的在这里:E:\tools\tomcat\apache-tomcat-8.5.38\conf 改其中的参数 修改前  java.u ...

  8. Nginx反向代理Odoo并转为https

    生成证书文件 生成自签名证书,并放在指定位置 $ openssl req -x509 -days 3650 -subj '/CN=odoo.youyun.com/' -nodes -newkey rs ...

  9. HDU - 6128

    题意略: 题解:二次剩余板子题 //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optimize(4) //#pra ...

  10. 容斥原理——hdu1796

    /* 遇到这种题一般用dfs,枚举起点来做 但是本题如何进行容斥? 比如以x为起点,第一步dfs到y,那么因子有lcm(x,y)的 所有数要被减掉(容斥中偶数是减法) 然后第二步dfs到z,那么因子有 ...