美文网首页Leetcode
Leetcode 506. Relative Ranks

Leetcode 506. Relative Ranks

作者: SnailTyan | 来源:发表于2021-08-26 09:19 被阅读0次

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

    1. Description

    Relative Ranks

    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

        本文链接:https://www.haomeiwen.com/subject/chwuvltx.html