Leetcode 506. Relative Ranks

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Relative Ranks

2. Solution

解析:Version 1,由于元素唯一,因此先对数组排序,然后构造有序数组与名次对应的字典,然后遍历数组得到对应的名词。

  • Version 1
1
2
3
4
5
6
7
8
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

Reference

  1. https://leetcode.com/problems/relative-ranks/
如果有收获,可以请我喝杯咖啡!