当前位置: 代码迷 >> 综合 >> Leetcode 506. Relative Ranks
  详细解决方案

Leetcode 506. Relative Ranks

热度:47   发布时间:2023-12-12 20:57:39.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/
  相关解决方案