当前位置:网站首页>506.相对名次
506.相对名次
2022-07-17 10:57:00 【有时候。】
1. 题目描述
给你一个长度为 n 的整数数组 score ,其中 score[i] 是第 i 位运动员在比赛中的得分。所有得分都 互不相同 。
运动员将根据得分决定名次 ,其中名次第 1 的运动员得分最高,名次第 2 的运动员得分第 2 高,依此类推。运动员的名次决定了他们的获奖情况:
名次第 1 的运动员获金牌 “Gold Medal” 。
名次第 2 的运动员获银牌 “Silver Medal” 。
名次第 3 的运动员获铜牌 “Bronze Medal” 。
从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 “x”)。
使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。
输入:score = [10,3,8,9,4]
输出:[“Gold Medal”,“5”,“Bronze Medal”,“Silver Medal”,“4”]
解释:名次为 [1st, 5th, 3rd, 2nd, 4th] 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/relative-ranks
2. 解题思路
对score数组以及对应的下标排序(之前写过根据列表A对列表B排序的博客),根据排序后的下标把名次分配到相应的位置即可。
- python
# 方法1:利用.index()找到原始score数组中的元素在排序后所得score数组中的下标(即名次),时间复杂度高
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
score_sorted = sorted(score)[::-1]
answer = []
medal = ["Gold Medal","Silver Medal","Bronze Medal"]
for x in score:
idx = score_sorted.index(x)
if idx < 3:
answer.append(medal[idx])
else:
answer.append(str(idx+1))
return answer
# 方法2:对score和下标同时排序,再根据排序后的下标分配对应的名次, 时间复杂度为排序的时间复杂度
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
answer = [0] * len(score)
rank = sorted([(score[i], i) for i in range(len(score))])[::-1]
for i in range(len(score)):
pair = rank[i]
if i == 0:
answer[pair[1]] = "Gold Medal"
elif i == 1:
answer[pair[1]] = "Silver Medal"
elif i == 2:
answer[pair[1]] = "Bronze Medal"
else:
answer[pair[1]] = str(i+1)
return answer
边栏推荐
- Series operation of vector container (detailed explanation)
- 565. 数组嵌套 / 剑指 Offer II 001. 整数除法
- Part I - Fundamentals of C language_ 1. Overview of C language
- 数据库概述
- Es restful operation
- 2022.7.16-----leetcode.剑指offer.041
- Fundamentals of C language -- 2-2 const keywords and pointers
- Pytorch calls cublasltmattmul to do GEMM and add bias. It's well written
- how to use culasLt
- 记忆 lda LDA in blas level-3 SGEMM cublasGemmex cubulasSgemm
猜你喜欢
![[C language] storage of shaping data](/img/50/5454826da9a671fff0138388d23469.png)
[C language] storage of shaping data

第一部分—C语言基础篇_4. 程序流程结构

Chapter 4 - first order multi-agent system consistency - > switching topology system consistency

第十章 STL 之 stack

rhcsa 第二天 7.15

Part I - Fundamentals of C language_ 3. Operators and expressions

负载型金属有机骨架ZIF-8/氧化石墨烯储氢材料|二氧化钛/ZIF-8复合材料|二氧化硅@ZIF8纳米材料

将视频格式转换为gif图片格式
![[C language] summary of array knowledge points](/img/82/f3b76650fe8390f8929b2d3134b01d.png)
[C language] summary of array knowledge points

Among China's top ten national snacks, it is actually the first
随机推荐
实验1:使用Matlab工具箱进行相机标定实验
Chapter 13 set/ multiset of STL
[C language] summary of function knowledge points
[hero planet July training leetcode problem solving daily] 17th kuansou
MySQL -- SQL optimization case -- implicit character encoding conversion
第一部分—C语言基础篇_6. 函数
Part I - Fundamentals of C language_ 5. Arrays and strings
How to correctly execute jedis unit test
18、shell脚本编程(1)
The inflection point of eth may be just around the corner, which is how to
【洛谷】P2357 守墓人
Chapter 12 list of STL
Go exceed API source code reading (II) -- openFile ()
Rhcsa jour 2 7,15
氨基的金属-有机骨架材料Fe-MOF,Fe-MIL-88NH2|Zr基金属-有机骨架催化剂(Pt-UiO-66)|齐岳生物
组件化高级--插槽
Anaconda与Jupyter Notebook入门级详细使用教程
Chapter 4 - first order multi-agent system consistency - > pilot follower system consistency
[C language] summary of array knowledge points
壳聚糖包裹PCN224纳米粒子|金属-有机骨架Fe-MIL-88NH2|镍基MOF材料(Ni-MOF/NF)