当前位置:网站首页>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
边栏推荐
- [Luogu] p2357 tomb keeper
- C language force buckle question 25 of K a group of inverted linked list. Multi pointer traversal
- 一汽丰田亚洲狮首次产品焕新
- Chapter 4 - first order multi-agent system consistency - > switching topology system consistency [program code]
- Duilib implements tooltip custom mouse prompt window
- Mux256to1v,Hadd,Fadd
- On the problem of dependency invalidation when the dependency in the basic module is inherited by the sub module in the microservice
- Part I - Fundamentals of C language_ 4. Procedure flow structure
- Es restful operation
- 【C语言】浮点型在内存的存储
猜你喜欢

The study found that DNA nano device injection can be safely used for medical purposes

【摸鱼神器】UI库秒变低代码工具——表单篇(二)子控件
[email protected]|[email protected]<"/>卟啉包裹型金属有机框架材料[email protected]|[email protected]<

实验1:使用Matlab工具箱进行相机标定实验

Es index, type (mapping), document, IK word breaker

Part I - Fundamentals of C language_ 5. Arrays and strings
![[C language] storage of floating-point type in memory](/img/a5/5e360fb0a1b3425dd9372cc29d0a3b.png)
[C language] storage of floating-point type in memory

面试题-给::memcpy函数设计测试用例

Convert video format to GIF picture format

Vim详解
随机推荐
组件化高级--插槽
Makefile中在命令前加上- 则忽略该命令产生的错误,继续执行下一条命令
中国十大国民小吃,第一居然是它
第一部分—C语言基础篇_3. 运算符与表达式
[C language] storage of shaping data
记忆 lda LDA in blas level-3 SGEMM cublasGemmex cubulasSgemm
rhcsa 第二天 7.15
Part I - Fundamentals of C language_ 4. Procedure flow structure
Develop the first Flink app
node+express搭建服务器环境
数组模拟队列
Chapter VIII vector of STL
C51 常见数据类型详解
壳聚糖包裹PCN224纳米粒子|金属-有机骨架Fe-MIL-88NH2|镍基MOF材料(Ni-MOF/NF)
一汽丰田亚洲狮首次产品焕新
Redis cache avalanche
关于基础模块中的依赖由微服务中的子模块继承的时候依赖失效的问题
D. Mark and Lightbulbs
氨基的金属-有机骨架材料Fe-MOF,Fe-MIL-88NH2|Zr基金属-有机骨架催化剂(Pt-UiO-66)|齐岳生物
R language data Table import data practice: data Table uses dcast data. The table function implements pivot table