506. 相对名次

给出 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。

所有运动员的成绩都不相同。

class Solution {
public String[] findRelativeRanks(int[] nums) {
int len = nums.length;
String[] result = new String[len];
if (len<1)return result;
//1、可以把数组转换成键值对(下标:成绩)按成绩排序,最后取出对应名次
//2、分数总量不大,不超过10000,可以用计数排序
int min = 0;
for (int num : nums) {
min = num>min?num:min;
}
int[] map = new int[min+1];
for (int num : nums) {
map[num]++;
}
//把map中每个分数对应的人数,转换成对应名次
int rank = 1;int temp = 0;
for (int i = min; i >= 0; i--) {
if (map[i]!=0){
temp = map[i];
map[i] = rank;
rank += temp;
}
}
//把名次映射成金银铜奖"Gold Medal", "Silver Medal", "Bronze Medal"
String s = "";int value = 0;
for (int i = 0; i < len; i++) {
value= map[nums[i]];
switch (value){
case 1: s = "Gold Medal";break;
case 2: s = "Silver Medal";break;
case 3: s = "Bronze Medal";break;
default: s = String.valueOf(value);
}
result[i] = s;
}
return result;
}
}

Java实现 LeetCode 506 相对名次的更多相关文章

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

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

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. (2)通信中为什么要进行AMC?

    AMC,Adaptive Modulation and Coding,自适应调制与编码. 通信信号的传输环境是变化不定的,信道环境时好时差.在这种情景下,我们不可能按照固定的MCS进行信号发送.假如信 ...

  2. css布局的漂浮、position定位

    float: left:文本流向对象的右边 right:文本流向对象的左边 div具有自动换行效果 position: absolute:将对象从文档流中拖出,即飘到最上面一层,形成层叠,不占位置:可 ...

  3. Java UDP小结

    UDP: * 发送端                                                                                           ...

  4. 设计模式之GOF23享元模式

    享元模式FlyWeight 场景:如果有很多个完全相同或者相似的对象,可以节省内存资源 核心: 享元模式以共享的方式高效地支持大量细粒对象的重用 享元对象做到共享的关键是区分了内部状态和外部状态: 内 ...

  5. [hihoCoder1236 Scores 2015BeijingOnline]简单粗暴的分块+简单粗暴的bitset

    题意:50000个5维向量,50000次询问每一维都不大于某一向量的向量个数,强制在线. 思路:做完这题才知道bitset效率这么高,自己本地测试了下1s可以操作1010个bit,orz简单粗暴 令S ...

  6. GIT代码版本管理

    一.实验目的 1.了解分布式版本控制系统的核心机理: 2.熟练掌握git的基本指令和分支管理指令: 二.实验内容 1.安装git: 2.初始配置git,git init git status指令: 3 ...

  7. mybatis开发,你用 xml 还是注解?我 pick ...

    最近在看公司项目时发现有的项目mybatis是基于注解开发的,而我个人的习惯是基于xml文件开发. 对于mybatis注解开发的原理理解不够,于是翻阅了部分源码,写下此文.主要介绍了mybatis开发 ...

  8. 让.NetCore程序跑在任何有docker的地方

    一.分别在Windows/Mac/Centos上安装Docker Windows上下载地址:https://docs.docker.com/docker-for-windows/install/(wi ...

  9. 数据结构----双端队列Dque

    双端队列的概念与数据结构 deque(也称为双端队列)是与队列类似的项的有序集合.它有两个端部,首部和尾部,并且项在集合中保持不变. deque 特殊之处在于添加和删除项是非限制性的.可以在前面或后面 ...

  10. 蓝桥杯 试题 历届试题 对局匹配 DP解决

    问题描述 小明喜欢在一个围棋网站上找别人在线对弈.这个网站上所有注册用户都有一个积分,代表他的围棋水平. 小明发现网站的自动对局系统在匹配对手时,只会将积分差恰好是K的两名用户匹配在一起.如果两人分差 ...