Leetcode 506. Relative Ranks

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 506. Relative Ranks相关的知识,希望对你有一定的参考价值。

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

1. Description

2. 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

Reference

  1. https://leetcode.com/problems/relative-ranks/

以上是关于Leetcode 506. Relative Ranks的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode&Python] Problem 506. Relative Ranks

[LeetCode] 506. Relative Ranks

leetcode 506. ????????????(Relative Ranks)

Leetcode 506. Relative Ranks

[LeetCode] 506. Relative Ranks_Easy tag: Sort

LeetCode算法题-Relative Ranks(Java实现)