Skip to content

Latest commit

 

History

History
55 lines (42 loc) · 2.08 KB

풀이_LC506.md

File metadata and controls

55 lines (42 loc) · 2.08 KB

🫖 506. Relative Ranks

  • Date : 2021.11.14(일)
  • Time : 20분

Problem

  • You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.
  • The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:
    • The 1st place athlete's rank is "Gold Medal".
    • The 2nd place athlete's rank is "Silver Medal".
    • The 3rd place athlete's rank is "Bronze Medal".
    • For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is "x").
  • Return an array answer of size n where answer[i] is the rank of the ith athlete.

Constraints

  • n == score.length
  • 1 <= n <= 10^4
  • 0 <= score[i] <= 10^6
  • All the values in score are unique.

Example

  • Input: score = [5,4,3,2,1]
  • Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
  • Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].

풀이

    def findRelativeRanks(self, score: list[int]) -> list[str]:
        myDict = {}
        myList = sorted(score, reverse=True)

        for i in range(0, len(myList)):
            if i == 0:
                myDict[myList[i]] = 'Gold Medal'
            elif i == 1:
                myDict[myList[i]] = 'Silver Medal'
            elif i == 2:
                myDict[myList[i]] = 'Bronze Medal'
            else:
                myDict[myList[i]] = str(i + 1)
        ret = []

        for i in range(0, len(score)):
            ret.append(myDict[score[i]])

        return ret
    
        

: 점수를 보고 등수를 정해주는 알고리즘이다. 먼저 score를 점수순으로 정렬해준다. 여기서 포인트는 원본을 살려두는 것이다. 그리고 점수를 보고 등수를 정하는데 그 등수를 dict type에 저장해둔다. key 와 value 값으로 나눠서 저장해준다. 그리고 for문을 통해 원래의 score를 보고 다시 재정렬 해준다.