给出 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. 侧滑关闭Activity的解决方案——SwipeBackLayout

    项目地址:ikew0ng/SwipeBackLayout: An Android library that help you to build app with swipe back gesture. ...

  2. dea死锁处理和大事务处理

    死锁处理流程: show full processlist; # 获得当前所有数据库连接 select id, db, user, host, command, time, state, info f ...

  3. 阿里云MaxCompute 2019-8月刊

    您好,MaxCompute 2019.8月刊为您带来8月产品.技术最新动态,欢迎阅读. 导读 [重要发布]8月产品重要发布 [文档更新]8月重要文档更新推荐 [干货精选]8月精选技术文章推荐 [精彩活 ...

  4. 平衡树模板【splay的实现】

    [平衡树splay实现] 无注释代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=1e ...

  5. MySQL中修改多个数据表的字段拼接问题

    错误1: 异常:Truncated incorrect DOUBLE value: 'lili' 问题分析:我的修改sql语句是:update video set vname='汉字' and vdi ...

  6. Django项目: 3.用户注册功能

    本章内容的补充知识点 导入库的良好顺序: 1.系统库 2.django库 3.自己定义的库(第三方库) redis缓存数据库的数据调用速度快,但是不利于长时间保存. mysql用于长时间存储,但是调用 ...

  7. hive设置列头(永久模式)

    到hive目录下的hive-site <property> <name>hive.cli.print.header</name> <value>true ...

  8. Java基础(业务问题)

    幂等的处理方式 一.查询与删除操作是天然幂等 二.唯一索引,防止新增脏数据 三.token机制,防止页面重复提交 四.悲观锁  for update 五.乐观锁(通过版本号/时间戳实现, 通过条件限制 ...

  9. CCA Spark and Hadoop 开发者认证技能点【2016只为hadoop达到巅峰】

    Required Skills 技能要求: Data Ingest 数据消化: The skills to transfer data between external systems and you ...

  10. 乐观、悲观锁、redis分布式锁

    悲观锁总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁(共享资源每次只给一个线程使用,其它线程阻塞,用完后再把资源转让给 ...