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. 走,去出海,一起“Copy to World” | 36氪出海行业报告

    http://www.sohu.com/a/200845344_114778 从工具类产品在海外聚集大量流量到新闻.社交游戏等内容类产品在海外取得优异成绩,中国正在完成从Copy to China向C ...

  2. Dubbo源码分析:设计总结

    设计原则 1.   多用组合,少用继承 2.   针对接口编程,不针对实现编程 3.   依赖抽象,不要依赖具体实现类. 设计模式 1.   策略设计模式:Dubbo扩展Spring的xml标签解析 ...

  3. 聚类------KNN

    import numpy as np from math import sqrt import operator as opt def normData(dataSet): maxVals = dat ...

  4. 抓取Dump文件的方法和工具介绍

    一.Windows系统的任务管理器里抓dump 启动任务管理器,选中某个进程,右键,弹出菜单"创建转储文件" 注意事项: 当你在64位Windows系统上抓32位进程的dmup文件 ...

  5. Cogs 1632. 搬运工(二分图最小点覆盖)

    搬运工 ★ 输入文件:worker.in 输出文件:worker.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 小涵向小宇推荐了一款小游戏. 游戏是这样的,在一个n*n的地 ...

  6. 洛谷 P4568 [JLOI2011]飞行路线 题解

    P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ...

  7. Python 元类使用讲解

    我要一大群的类都具有一种特点,我怎么给他们加上呢?模板嘛,我从这个模板创建一群类不就OK了?那就需要元类了. 定义一个元类(就是一个类的模板!莫多想,还要记住这是类级别的,不是对象级别的!):代码如下 ...

  8. 基于Linux(中标麒麟)上QT的环境搭建——解决cannot find lGL的问题

    接上一篇,QT在中标麒麟环境安装完成后遇到运行报错的问题 一.问题描述: 在中标麒麟上配置好QT的环境后,新建一个工程,不做其他的任何操作,直接运行都会报cannot find lGL的错误.如图所示 ...

  9. Myschool试题

    题目: 1.查询所有学生记录,包含年级名称2.查询S1年级下的学生记录 一.com.myschool.dao 1 BaseDao package com.myschool.dao; import ja ...

  10. Android中如何引入已经存在的标题栏

    在写程序中很多时候很多页面的标题布局大致都是一样的,这时候不需要挨着都写一遍,只需要写一个xml文件,在所需要的时候进行引用就可以了 只需要这一行代码: <include layout=&quo ...