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. bfs与dfs小结

    1,bfs适合状态容易存储的题目,如果状态比较难存储,就难以进行记忆化搜索,必然会难以bfs. (比如听说滑雪这个题你用bfs会死得很难看) 2,但是有些题目会很深(比如网格单源最短路),用dfs会跑 ...

  2. Truffle - 以太坊Solidity编程语言开发框架

    http://truffle.tryblockchain.org/ Truffle框架 Truffle是什么? Truffle是针对基于以太坊的Solidity语言的一套开发框架. 本身基于JavaS ...

  3. C++之Lambda研究

    目录 目录 1 1. 前言 1 2. 示例1 1 3. 示例2 2 4. 示例3 3 5. 示例4 3 6. 示例5 6 7. 匿名类规则 6 8. 参考资料 7 1. 前言 本文代码测试环境为“GC ...

  4. Time of Trial

    Time of Trial(思维) 题目大意:一个人想逃,一个人要阻止那个人逃跑 ,阻止者念每一个符咒的时间为b[i],逃跑者念每一个符咒的时间为a[i],逃跑者如果念完了第i个符咒,阻止者还没有念完 ...

  5. (LIS)最长上升序列(DP+二分优化)

    求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...

  6. go的接口内部实现

    1 前言 1.1 Go汇编 Go语言被定义为一门系统编程语言,与C语言一样通过编译器生成可直接运行的二进制文件.这一点与Java,PHP,Python等编程语言存在很大的不同,这些语言都是运行在基于C ...

  7. C Primer Plus--结构和其他数据类型(2)

    目录 枚举类型 enumerated type 枚举默认值 为枚举指定值 命名空间 namespace typedef关键字 * () []修饰符 typedef与这三个运算符结合 函数与指针 函数指 ...

  8. 《JAVA程序设计》_第十周学习总结

    一.学习内容 12.1进程与进程 程序是一段静态的代码,进程是程序的一次动态执行过程,这个过程也是进程本身从产生.发展至消亡的过程. 线程不是进程,是比进程更小的执行单位.但与进程不同的是,线程的中断 ...

  9. hadoop综合

    对CSV文件进行预处理生成无标题文本文件,将爬虫大作业产生的csv文件上传到HDFS 首先,我们需要在本地中创建一个/usr/local/bigdatacase/dataset文件夹,具体的步骤为: ...

  10. 第06组 Beta冲刺(1/5)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 准备beta冲刺的内容和分工 修改了后端的一些bug GitHub签入记录 接下来的计划 ...