原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description

题目:

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:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

题解:

把nums sort同时要保留index信息. 按照index给result的对应index赋值.

Time Complexity: O(nlogn), n = nums.length. Space: O(n).

AC Java:

 public class Solution {
public String[] findRelativeRanks(int[] nums) {
int [][] pair = new int[nums.length][2];
for(int i = 0; i<nums.length; i++){
pair[i][0] = nums[i];
pair[i][1] = i;
} Arrays.sort(pair, (a, b)->(b[0]-a[0]));
String [] res = new String[nums.length];
for(int i = 0; i<nums.length; i++){
if(i == 0){
res[pair[i][1]] = "Gold Medal";
}else if(i == 1){
res[pair[i][1]] = "Silver Medal";
}else if(i == 2){
res[pair[i][1]] = "Bronze Medal";
}else{
res[pair[i][1]] = String.valueOf(i+1);
}
}
return res;
}
}

LeetCode Relative Ranks的更多相关文章

  1. [LeetCode] Relative Ranks 相对排名

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

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

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

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

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

  4. [LeetCode&Python] Problem 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

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

  6. 506. Relative Ranks

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

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

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

  8. LeetCode算法题-Relative Ranks(Java实现)

    这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个 ...

  9. 506 Relative Ranks 相对名次

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

随机推荐

  1. 实现对第三方应用任意SO注入

    实现对第三方应用任意SO注入 0x01 应用在Android中运行,从外部对该进程可以进行任意SO文件动态注入,就是应用动态运行我们的SO文件 0x02 基本的逻辑是: 1.    获取目标进程的pi ...

  2. smarty变量调节器与函数

    smarty自带了一些变量调节器与内置函数,都在libs/plugins目录下,变量调节器以modifier开头,函数以function开头,而且我们可以自定义变量调节器与函数,熟练运用之后会极大地提 ...

  3. SQL语句 自连表查询。inner join用法,partition by ,列转行查询

    use mydb1 go -- 表T_Employee2 -- Id Name Position Dept -- 1 张三 员工 市场部 -- 2 李四 经理 销售部 -- 3 王五 经理 市场部 - ...

  4. 转:Windows下USB接口驱动技术(二)

  5. 虚拟机(VMWare)NAT 模式,配置静态IP上网的问题

    问题描述: 感觉问题解决了回过头来想就很简单,但是没解决就怎么也找不到问题,知识储备捉襟见肘.针对这个问题我好长时间才弄好,各种找资料,也证明本人筛选有用博客的能力比较低,先让我哭会去…… 在虚拟的实 ...

  6. Shell 语句

    一 test 测试: 测试命令 test [ ] [[ ]] (( ))打开man test 逐一介绍每个参数 浮点计算:echo 'scale=2;1/3'|bc -l  测试操作命令执行后会返回到 ...

  7. apache2.4配置ssl

    1,yum 安装openssl和openssl-devel,httpd-devel2,生成证书(也可以从公司的证书颁发机构获取): #建立服务器密钥 openssl genrsa -des3 > ...

  8. MongoDB 使用Limit和Skip完成分页 和游标(二)

    //$slice操作符返回文档中指定数组的内部值 //查询出Jim书架中第2~4本书 db.persons.find({name:"jim"},{books:{"$sli ...

  9. Go goroutine (协程)

    在Go语言中goroutine是一个协程,但是跟Python里面的协程有很大的不同: 在任何函数前只需要加上go关键字就可以定义为协程; 不需要在定义时区分是否是异步函数  VS  async def ...

  10. Eclipse引入BASE64Encoder的问题

    在代码中引用了BASE64Encoder,上面显示的错误信息如下: Access restriction: The type BASE64Encoder is not accessible due t ...