Skip to content

Commit 72e88be

Browse files
solves relative ranks in python
1 parent 9fe8bb1 commit 72e88be

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/KeyBoardRow.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/keyboard_row.py) |
135135
| 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FindModeInBinarySearchTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/find_mode_in_binary_search_tree.py) |
136136
| 504 | [Base 7](https://leetcode.com/problems/base-7) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/Base7.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/base_7.py) |
137-
| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks) | Easy | |
137+
| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks) | Easy | [![Python](https://img.icons8.com/color/35/000000/python.png)](python/relative_ranks.py) |
138138
| 507 | [Perfect Number](https://leetcode.com/problems/perfect-number) | Easy | |
139139
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | Easy | |
140140
| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital) | Easy | |

Diff for: python/relative_ranks.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def get_rank(self, position: int) -> str:
6+
if position == 1: return 'Gold Medal'
7+
if position == 2: return 'Silver Medal'
8+
if position == 3: return 'Bronze Medal'
9+
return str(position)
10+
11+
def findRelativeRanks(self, score: List[int]) -> List[str]:
12+
numbers = [(index, val) for index, val in enumerate(score)]
13+
numbers.sort(key=lambda x: x[1], reverse=True)
14+
value_to_index = {}
15+
for index, (_, value) in enumerate(numbers):
16+
value_to_index[value] = index
17+
result = [''] * len(score)
18+
for index, value in enumerate(score):
19+
result[index] = self.get_rank(value_to_index[value] + 1)
20+
return result

0 commit comments

Comments
 (0)