[LeetCode&Python] Problem 506. Relative Ranks
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 1:
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.
Note:
- N is a positive integer and won't exceed 10,000.
- All the scores of athletes are guaranteed to be unique.
class Solution(object):
def findRelativeRanks(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
n=len(nums)
if n==0:
return nums
elif n==1:
return ['Gold Medal']
elif n==2:
if nums[0]>nums[1]:
return ['Gold Medal','Silver Medal']
else:
return ['Silver Medal','Gold Medal']
ans=[]
s=sorted(nums)
a1=s[-1]
a2=s[-2]
a3=s[-3] for i in nums:
if i==a1:
ans.append('Gold Medal')
elif i==a2:
ans.append('Silver Medal')
elif i==a3:
ans.append('Bronze Medal')
else:
ans.append(str(n-s.index(i)))
return ans
[LeetCode&Python] Problem 506. Relative Ranks的更多相关文章
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- [LeetCode&Python] Problem 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
随机推荐
- 数组Array.sort()排序的方法
数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...
- vue element-ui 通过v-for渲染的el-form-item组件,使用自带的表单验证
HTML: <el-form ref="newTermDetail" :model="newTermDetail" class="auto_fo ...
- Python3+BaiduAI识别高颜值妹子图片
一.在百度云平台创建应用 为什么要到百度云平台创建应用,首先来说是为了获取获取access_token时需要的API Key和Secret Key 至于为什么需要API Key和Secret Key才 ...
- maven-assembly-plugin
<build> <finalName>detail</finalName> <plugins> <plugin> <artifactI ...
- py requests.post2
# -*- coding: utf-8 -*-import jsonimport requestsheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 6 ...
- grep console
FF6464 9F6B00 8A8A00 4B5E76 8A7674 980B4F 980B4F
- mysql如何让自增id从某个位置开始设置方法
一般情况下两种方式: 1.本地数据不需要的情况下直接情况表(尽量不使用
- linux 安装 DenyHosts 防止密码被暴力破解
DenyHosts DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件 ...
- bzoj1798
题解: 同洛谷2023 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; ; ll p,v,su ...
- (C/C++学习笔记) 八. 程序控制语句
八. 程序控制语句 ● 基础知识 算法的基本控制结构: 顺序结构(sequential structure), 选择结构(case structure), 循环结构(loop structure) c ...