Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- github
- ubuntu
- programmers
- 코로나19
- 편스토랑
- dacon
- 맥북
- 백준
- Git
- 더현대서울 맛집
- hackerrank
- leetcode
- 프로그래머스
- 우분투
- 편스토랑 우승상품
- 파이썬
- 금융문자분석경진대회
- PYTHON
- gs25
- 프로그래머스 파이썬
- Kaggle
- 데이콘
- Baekjoon
- 캐치카페
- SW Expert Academy
- AI 경진대회
- 자연어처리
- ChatGPT
- Real or Not? NLP with Disaster Tweets
- Docker
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 506. Relative Ranks (Python) 본문
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:
- N is a positive integer and won't exceed 10,000.
- 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
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1337. The K Weakest Rows in a Matrix (Python) (0) | 2020.09.09 |
---|---|
[leetCode] 1331. Rank Transform of an Array (Python) (0) | 2020.09.08 |
[leetCode] 1507. Reformat Date (Python) (0) | 2020.09.06 |
[leetCode] 415. Add Strings (Python) (0) | 2020.09.05 |
[leetCode] 917. Reverse Only Letters (Python) (0) | 2020.09.04 |
Comments