관리 메뉴

솜씨좋은장씨

[leetCode] 506. Relative Ranks (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 506. Relative Ranks (Python)

솜씨좋은장씨 2020. 9. 7. 00:15
728x90
반응형

Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

 

Example 1:

Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
For the left two athletes, you just need to output their relative ranks according to their scores.

Note:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

Solution

class Solution:
    def findRelativeRanks(self, nums: List[int]) -> List[str]:
        sorted_nums = sorted(nums, reverse=True)
        
        answer = []
        
        medal = ["Gold Medal", "Silver Medal", "Bronze Medal"]
        
        rank_dict = {}
        
        for i in range(len(sorted_nums)):
            rank_dict[sorted_nums[i]] = i
        
        for i in range(len(nums)):
            if rank_dict[nums[i]] == 0:
                answer.append("Gold Medal")
            elif rank_dict[nums[i]] == 1:
                answer.append("Silver Medal")
            elif rank_dict[nums[i]] == 2:
                answer.append("Bronze Medal")
            else:
                answer.append(str(rank_dict[nums[i]] + 1))
            
        return answer

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments