文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
Relative Ranks2. Solution
解析:Version 1,由于元素唯一,因此先对数组排序,然后构造有序数组与名次对应的字典,然后遍历数组得到对应的名词。
- Version 1
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
n = len(score)
ranks = ["Gold Medal", "Silver Medal", "Bronze Medal"]
ranks += [str(i) for i in range(4, n+1)]
mapping = dict(zip(sorted(score, reverse=True), ranks))
result = [mapping[score[i]] for i in range(n)]
return result
网友评论