Skip to content

Commit 44f4fd6

Browse files
author
Daniel Nallapalli
committed
Explanation #506 - Daniel - 16/06/2025 - commit #9
1 parent 653048a commit 44f4fd6

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# 506. Relative Ranks
2+
3+
**Difficulty:** *Easy*
4+
**Category:** *Arrays, Sorting*
5+
**Leetcode Link:** [Problem Link](https://leetcode.com/problems/relative-ranks/description/)
6+
7+
---
8+
9+
## 📝 Introduction
10+
11+
*Given a list of scores representing the performance of athletes, the task is to assign ranks to them based on their scores. The top three scorers receive "Gold Medal", "Silver Medal", and "Bronze Medal" respectively, and the rest receive their numeric rank (starting from 4th place onward).*
12+
13+
*Constraints typically include:<br>
14+
Each score is a unique positive integer. <br>
15+
The output should be a list of strings corresponding to each athlete's rank.*
16+
17+
---
18+
19+
## 💡 Approach & Key Insights
20+
21+
*The core idea is to:<br>
22+
Track the original indices of scores.<br>
23+
Sort the scores in descending order.<br>
24+
Assign ranks based on sorted positions, updating the original indices accordingly.<br>
25+
This allows easy mapping of ranks back to their original positions in the input array.*
26+
27+
---
28+
29+
## 🛠️ Breakdown of Approaches
30+
31+
### 1️⃣ Brute Force / Naive Approach
32+
33+
- **Explanation:** *A brute force way would be to compare each score with all others to count how many scores are higher. The rank is then one more than the number of scores higher than it. This would take O(n²) time.*
34+
- **Time Complexity:** *O(n²) - due to nested comparisons.*
35+
- **Space Complexity:** *O(n) - for storing result.*
36+
- **Example/Dry Run:**
37+
38+
```plaintext
39+
Example input: [10, 3, 8, 9, 4]
40+
41+
Step 1: Count how many scores are greater for each score
42+
Step 2: Assign rank accordingly
43+
Step 3: Result = ["Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"]
44+
```
45+
46+
### 2️⃣ Optimized Approach
47+
48+
- **Explanation:** *Use enumerate to pair each score with its original index. Sort the list in descending order based on score. Then iterate through the sorted list and assign ranks based on index positions.*
49+
- **Time Complexity:** *O(n log n) – due to sorting.*
50+
- **Space Complexity:** *O(n) – for storing result and indexed list.*
51+
- **Example/Dry Run:**
52+
53+
```plaintext
54+
Example input: [10, 3, 8, 9, 4]
55+
56+
Step 1: indexed_scores = [(10,0), (3,1), (8,2), (9,3), (4,4)]
57+
Step 2: After sorting = [(10,0), (9,3), (8,2), (4,4), (3,1)]
58+
Step 3: Assign:
59+
60+
Rank 0: index 0 → "Gold Medal"
61+
Rank 1: index 3 → "Silver Medal"
62+
Rank 2: index 2 → "Bronze Medal"
63+
Rank 3: index 4 → "4"
64+
Rank 4: index 1 → "5"
65+
66+
Final output: ["Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"]
67+
```
68+
69+
---
70+
71+
## 📊 Complexity Analysis
72+
73+
| Approach | Time Complexity | Space Complexity |
74+
| ------------- | --------------- | ---------------- |
75+
| Brute Force | O(n²) | O(n) |
76+
| Optimized | O(n log n) | O(n) |
77+
78+
---
79+
80+
## 📉 Optimization Ideas
81+
82+
*The optimized solution is efficient and doesn't need further improvement.<br>
83+
However, using a priority queue or heap could also be considered but adds complexity without clear benefit for this problem.*
84+
85+
---
86+
87+
## 📌 Example Walkthroughs & Dry Runs
88+
89+
90+
```plaintext
91+
Example:
92+
Input: [5, 4, 3, 2, 1]
93+
94+
Process:
95+
Step 1: Pair with index → [(5,0), (4,1), (3,2), (2,3), (1,4)]
96+
Step 2: Sort by score descending → [(5,0), (4,1), (3,2), (2,3), (1,4)]
97+
Step 3: Assign ranks:
98+
0 → "Gold Medal"
99+
1 → "Silver Medal"
100+
2 → "Bronze Medal"
101+
3 → "4"
102+
4 → "5"
103+
104+
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
105+
```
106+
107+
---
108+
109+
## 🔗 Additional Resources
110+
111+
- [Resource 1](https://docs.python.org/3/library/functions.html#enumerate)
112+
- [Resource 2](https://docs.python.org/3/library/functions.html#sorted)
113+
114+
---
115+
116+
Author: Daniel Nallapalli <br>
117+
Date: 16/06/2025

0 commit comments

Comments
 (0)