Skip to content

Commit 913cbb6

Browse files
authored
Create relative-ranks.py
1 parent baad7d4 commit 913cbb6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Python/relative-ranks.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
# Given scores of N athletes, find their relative ranks and
5+
# the people with the top three highest scores, who will be
6+
# awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".
7+
#
8+
# Example 1:
9+
# Input: [5, 4, 3, 2, 1]
10+
# Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
11+
# Explanation: The first three athletes got the top three highest scores,
12+
# so they got "Gold Medal", "Silver Medal" and "Bronze Medal".
13+
# For the left two athletes, you just need to output
14+
# their relative ranks according to their scores.
15+
# Note:
16+
# N is a positive integer and won't exceed 10,000.
17+
# All the scores of athletes are guaranteed to be unique.
18+
19+
class Solution(object):
20+
def findRelativeRanks(self, nums):
21+
"""
22+
:type nums: List[int]
23+
:rtype: List[str]
24+
"""
25+
sorted_nums = sorted(nums)[::-1]
26+
ranks = ["Gold Medal", "Silver Medal", "Bronze Medal"] + map(str, range(4, len(nums) + 1))
27+
return map(dict(zip(sorted_nums, ranks)).get, nums)

0 commit comments

Comments
 (0)