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 ...
随机推荐
- 「APIO 2019」奇怪装置
题目 考虑推柿子 最开始的想法是如果两个\(t\)在\(mod\ B\)意义下相等,那么只需要比较一下\((t+\left \lfloor \frac{t}{B}\rfloor \right)mod\ ...
- java_Map集合
import java.util.HashMap; public class MapTest { /** * 1.Map集合是双列几个,一个元素包含两个值(key,value) * 2.Map集合中的 ...
- MVC中DropDownListFor的使用注意事项
1.在MVC的View页面中使用DropDownListFor时当DropDownListFor是列表是通过后台ViewBag传过来时,当ViewBag中的Key与DropDownListFor一致时 ...
- Android开发 输入法调用学习
方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context. ...
- Loadrunner系列学习--Loadrunner架构(1)
Loadrunner系列学习--Loadrunner架构(1) 最近在学习Loadrunner,发现一个英文网站http://www.wilsonmar.com/1loadrun.htm里面介绍的比较 ...
- 廖雪峰Java15JDBC编程-3JDBC接口-2JDBC查询
我们可以使用JDBC查询来执行select语句. 1. Statement try(Connection conn = DriverManager.getConnection(JDBC_URL, JD ...
- fetchSql是3.2.3新增的连贯操作方法,用于直接返回SQL而不是执行查询。
fetchSql是3.2.3新增的连贯操作方法,用于直接返回SQL而不是执行查询. 例如: $result = M('User')->fetchSql(true)->find(1); 输出 ...
- [复习]平衡树splay
明天要考试了…… 出来写一个splay的复习总结. 怕忘…… ^废话^ 以下内容学习自yyb大神的博客, 由于yyb大神内容不全, 部分是博主本人自行脑补... 这个模板还是比较全的^-^ ^又是一堆 ...
- DuiLib学习笔记1.编译运行demo
c++中皮肤问题比较麻烦,MFC自带的太难用.DirectUI界面库就比较强大了,之前像skin++之类的基于DirectUI收费昂贵.DuiLib是基于DirectUI的界面库,可以将用户界面和处理 ...
- CF1158F Density of subarrays
CF1158F Density of subarrays 首先可以发现,有值的p最大是n/c 对于密度为p,每个数至少出现c次,且其实是每出现c个数,就分成一段,这样贪心就得到了p %ywy n/c ...