作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: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:

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. Output:
7

Note:

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

题目大意

把一个数组中最大的三个位置设置成金银铜奖,其他位置是当前数字的排名。

解题方法

排序

java也可以通过sort的方法,把类似键值对的数组按照第一维排序,第二维也会跟着排序:

Example:

nums[i]    : [10, 3, 8, 9, 4]
pair[i][0] : [10, 3, 8, 9, 4]
pair[i][1] : [ 0, 1, 2, 3, 4] After sort:
pair[i][0] : [10, 9, 8, 4, 3]
pair[i][2] : [ 0, 3, 2, 4, 1]

这样就可以找出前几个较大值对应的序号,从而标出名词。

参考这个详细解答:https://discuss.leetcode.com/topic/77876/easy-java-solution-sorting

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][4] = i;
}
Arrays.sort(pair, (a, b) -> (b[0] - a[0]));
String[] ans = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
if (i == 0) {
ans[pair[i][5]] = "Gold Medal";
} else if (i == 1) {
ans[pair[i][6]] = "Silver Medal";
} else if (i == 2) {
ans[pair[i][7]] = "Bronze Medal";
} else {
ans[pair[i][8]] = "" + (i + 1);
}
}
return ans;
}
}

argsort

这个题,因为我用python做kNN的时候也要找出前几个大值所在的序号,就用了numPy的argsort()函数。LeetCode也可以用Numpy的!!看我的解法!!

import numpy as np
class Solution:
def findRelativeRanks(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
ranks = np.argsort(np.array(nums))[::-1]
N = len(nums)
res = list(map(str, ranks))
for r in range(N):
if r == 0:
res[ranks[0]] = "Gold Medal"
elif r == 1:
res[ranks[1]] = "Silver Medal"
elif r == 2:
res[ranks[2]] = "Bronze Medal"
else:
res[ranks[r]] = str(r + 1)
return res

import numpy as np
class Solution:
def findRelativeRanks(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
heap = [(-num, i) for i, num in enumerate(nums)]
heapq.heapify(heap)
N = len(nums)
res = [""] * N
count = 1
while heap:
num, i = heapq.heappop(heap)
if count == 1:
res[i] = "Gold Medal"
elif count == 2:
res[i] = "Silver Medal"
elif count == 3:
res[i] = "Bronze Medal"
else:
res[i] = str(count)
count += 1
return res

日期

2017 年 4 月 14 日
2018 年 11 月 16 日 —— 又到周五了!

【LeetCode】506. Relative Ranks 解题报告(Python)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. 【leetcode】506. Relative Ranks

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

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

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

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

随机推荐

  1. Shell 变量嵌套

    实现:eval 1 a="indv1" 2 indv1="Sus1" 3 4 eval tmp='$'$a 5 echo $tmp //这里 echo 返回值为 ...

  2. perl 数组快速去除重复元素

    这里记录两种perl数组去重的办法,一种利用哈希(hash),一种直接利用perl自带的模块List::MoreUtils内部的函数uniq. 一.利用hash去重 示例代码如下: 1 #!/usr/ ...

  3. php header下载文件 无法查看原因

    php header下载文件 无法查看原因 php header下载文件 下方函数可以下载单个文件 function download($file_url){ if(!isset($file_url) ...

  4. java中的Arrays类

    今天刚接触了数组,学到了几个比较常用的方法 Fill方法:给数组赋值 sort方法:给数组升序 equals方法:比较数组中元素 值是否相等 binarySearch方法:对排序好的数组进行二分查找法 ...

  5. Qemu/kvm虚拟化源码解析学习视频资料

    地址链接:tao宝搜索:Linux云计算KVM Qemu虚拟化视频源码讲解+实践​https://item.taobao.com/item.htm?ft=t&id=646300730262 L ...

  6. TD课程通最终版本体验

    功能上,新版本增加了学校教室的上课情况,有无课程可以清楚查询,如下图: 在添加课程的设置上有改进,相比于之前编辑课程后不能保存,新版本在可保存的基础上又增加了登陆教务系统的功能,学生使用更加方便快捷, ...

  7. day25 组合和内置函数

    day25 组合和内置函数 一.组合 # 解决类与类之间代码冗余问题: 1. 继承 2. 组合 组合:一个对象拥有一个属性, 属性的值必须是另外一个对象 继承满足的是:什么是什么的关系 # is-a ...

  8. 【Git项目管理】git新手入门——基础教程

    一.Git工作流程 直接上手看图,了解Git工具的工作流程: 以上包括一些简单而常用的命令,但是先不关心这些,先来了解下面这4个专有名词. Workspace:工作区 Index / Stage:暂存 ...

  9. Linux:-e、-d、-f、-L、-r、-w、-x、-s、-h、

    -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fil ...

  10. 一、手把手教你docker搭建fastDFS文件上传下载服务器

    在搭建fastDFS文件上传下载服务器之前,你需要准备的有一个可连接的linux服务器,并且该linux服务器上已经安装了docker,若还有没安装docker的,先百度自行安装docker. 1.执 ...