-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0506-relative-ranks.py
More file actions
51 lines (43 loc) · 1.43 KB
/
0506-relative-ranks.py
File metadata and controls
51 lines (43 loc) · 1.43 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# time complexity: O(n)
# space complexity: O(n)
import heapq
from typing import List
class Solution:
def findRelativeRanks(self, scores: List[int]) -> List[str]:
medal = ["Gold Medal", "Silver Medal", "Bronze Medal"]
rankScore = scores.copy()
rankScore.sort(reverse=True)
rankList = []
for score in scores:
rank = rankScore.index(score) + 1
if 1 <= rank <= 3:
rankList.append(medal[rank - 1])
else:
rankList.append(str(rank))
return rankList
# time complexity: O(nlogn)
# space complexity: O(n)
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
N = len(score)
# Create a heap of pairs (score, index)
heap = []
for index, score in enumerate(score):
heapq.heappush(heap, (-score, index))
# Assign ranks to athletes
rank = [0] * N
place = 1
while heap:
original_index = heapq.heappop(heap)[1]
if place == 1:
rank[original_index] = "Gold Medal"
elif place == 2:
rank[original_index] = "Silver Medal"
elif place == 3:
rank[original_index] = "Bronze Medal"
else:
rank[original_index] = str(place)
place += 1
return rank
score = [10, 3, 8, 9, 4]
print(Solution().findRelativeRanks(score))