506. 相对名次

506. Relative Ranks

题目描述

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

(注: 分数越高的选手,排名越靠前。)

每日一算法2019/6/11Day 39LeetCode506. Relative Ranks

示例 1:

输入: [5, 4, 3, 2, 1]
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予“金牌”,“银牌”和“铜牌”("Gold Medal", "Silver Medal" and "Bronze Medal")。
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。

提示:

  1. N 是一个正整数并且不会超过 10000。
  2. 所有运动员的成绩都不相同。

Java 实现

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; class Solution {
public String[] findRelativeRanks(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
int n = nums.length;
String[] res = new String[n];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(nums[i], i);
}
Arrays.sort(nums);
for (int i = n - 1; i >= 0; i--) {
String str = String.valueOf(n - i);
if (n - i == 1) {
str = "Gold Medal";
} else if (n - i == 2) {
str = "Silver Medal";
} else if (n - i == 3) {
str = "Bronze Medal";
}
res[map.get(nums[i])] = str;
}
return res;
}
}

参考资料

LeetCode 506. 相对名次(Relative Ranks) 39的更多相关文章

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

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

  2. Java实现 LeetCode 506 相对名次

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

  3. 【leetcode】506. Relative Ranks

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

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

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

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

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

  6. 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 相对排名

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

  8. LeetCode Relative Ranks

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

  9. [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 ...

随机推荐

  1. learning java FileVisitor 遍丽文件及路径

    import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttribut ...

  2. 关于System.MissingMethodException异常

    什么是MissingMethodException 试图动态访问不存在的方法时引发的异常. 继承 Object Exception SystemException MemberAccessExcept ...

  3. Hungry Canadian

    Hungry Canadian(简单dp) 具体见代码注释 #include <iostream> #include <cstdio> #include <cstring ...

  4. mysql .字符串转日期

    insert into share (uid, mapId, isdir, type, pwd, shareTime, overTime, price) values (1, 10, 0, 1,&qu ...

  5. eclipse为项目设置jdk

    1)在项目上右键选中properties,会进入如下界面 (2)然后点击Add Library,进入设置Library的界面 (3)选中JRE System Library进入下一界面就可以设置jdk ...

  6. 【AtCoder】 ARC 102

    link C-Triangular Relationship 发现要么全部是\(K\)的倍数,要么全部是模\(K\)余\(K/2,(K=2n)\) #include<bits/stdc++.h& ...

  7. [HAOI2018]染色(NTT)

    前置芝士 可重集排列 NTT 前置定义 \[\begin{aligned}\\ f_i=C_m^i\cdot \frac{n!}{(S!)^i(n-iS)!}\cdot (m-i)^{n-iS}\\ ...

  8. 大量数据通过Phoenix插入到hbase报错记录(2)

    错误: Caused by: java.sql.SQLException: ERROR (INT10): Unable to find cached index metadata 解决办法: 在hba ...

  9. 让你的shell更体贴

    使用shell的时候很多,特别是拉幕式终端,使用时更加方便,同时可以利用 echo "Did you know that:" ;whatis $(ls /bin | shuf -n ...

  10. MongoDB 高级查询_aggregate聚合管道

    MongoDB 聚合管道(AggregationPipeline) 使用聚合管道可以对集合中的文档进行变换和组合.实际项目应用主要是表关联查询.数据的统计. MongoDB 中使用 db.COLLEC ...