Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals:
"Gold Medal", "Silver Medal" and "Bronze Medal".
Example 1:
Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal".
For the left two athletes, you just need to output their relative ranks according to their scores.
Note:
N is a positive integer and won't exceed 10,000.
All the scores of athletes are guaranteed to be unique.

思路:

思路很朴素,首先就是排序,然后定义一个map,储存对应分数及其排名信息。最后再遍历一遍原来的分数数组,对应每一个分数

给定相应的排名。注意,要提前备份原来的数组,因为排序之后数组顺序就变了。

vector<string> findRelativeRanks(vector<int>& nums)
{
vector<int>backup = nums;
vector<string>result;
if(nums.size()==)return result;
sort(nums.begin(),nums.end(),greater<int>());
map<int,string>table;
char rank[];
for(int i = ;i < nums.size();i++)
{
sprintf(rank,"%d",i+);
if(i == )table[nums[i]]="Gold Medal";
else if(i == )table[nums[i]]="Silver Medal";
else if(i == )table[nums[i]]="Bronze Medal";
else table[nums[i]] = rank;
}
result.push_back(table[backup[]]);
for(int i =;i<nums.size();i++)
{
result.push_back(table[backup[i]]);
}
return result;
}

[leetcode-506-Relative Ranks]的更多相关文章

  1. 【leetcode】506. Relative Ranks

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

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

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

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

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

  4. 506. Relative Ranks

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

  5. [LeetCode] 506. Relative Ranks_Easy tag: Sort

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

  6. 506 Relative Ranks 相对名次

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

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

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

  8. [LeetCode] Relative Ranks 相对排名

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

  9. LeetCode Relative Ranks

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

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

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

随机推荐

  1. Redis入门学习

    一.摘要 二.五种数据类型的基本命令操作 三.Redis连接池 四.普通同步方式 五.事务方式(Transactions) 六.管道(Pipelining) 七.管道中调用事务 八.分布式直连同步调用 ...

  2. 精益IT组织与分享式领导

    精益IT组织       未来的组织将专注于同行业的产品或业务流--其他的一切,包括专家和管理者在内,都是为了让一线工作人员可以第一时间就做好,而又不会遇到任何麻烦.最大的制约不是技术:真正的挑战是变 ...

  3. CPU最核心的电子元件叫做石英晶振

    CPU是电子计算机的主要设备之一,是电脑中的核心配件.主要功能是解释计算机指令以及处理计算机软件中的数据.有人会问,你知道CPU里面都有什么吗?我想大家都会说硅晶体,集成度极大的半导体材料.却没有人提 ...

  4. Dockerfile 最佳实践

    之前 一篇文章介绍 docker 的镜像基本原理和概念 ,主要介绍在编写 docker 镜像的时候一些需要注意的事项和推荐的做法. 虽然 Dockerfile 简化了镜像构建的过程,并且把这个过程可以 ...

  5. JBoss7安装、测试、配置和启动以及停止,部署

    转:http://www.hongyanliren.com/2014m01/3013.html 内容概要 JBoss系列三主要目的是演示如何部署应用到JBoss7/WildFly,如下图中描述了部署应 ...

  6. 《JAVA与模式》之命令模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述命令(Command)模式的: 命令模式属于对象的行为模式.命令模式又称为行动(Action)模式或交易(Transaction)模式. ...

  7. Fitting Bayesian Linear Mixed Models for continuous and binary data using Stan: A quick tutorial

    I want to give a quick tutorial on fitting Linear Mixed Models (hierarchical models) with a full var ...

  8. rPithon vs. rPython(转)

    Similar to rPython, the rPithon package (http://rpithon.r-forge.r-project.org) allows users to execu ...

  9. java-web中生成文档(一)

    基于Java的解决方案也是很多的,包括使用Jacob.Apache POI.Java2Word.iText等各种方式,其实在从Office 2003开始,就可以将Office文档转换成XML文件,这样 ...

  10. 【2017-05-30】WebForm文件上传

    用 FileUpload控件进行上传文件. <asp:FileUpload ID="FileUpload1"  runat="server" /> ...