Skip to content

Commit f4fa18b

Browse files
authored
Merge pull request #2028 from Saipradyumnagoud/main
Added 506-relative-ranks
2 parents 2e0d954 + af6553f commit f4fa18b

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
id: relative-ranks
3+
title: Relative Ranks
4+
level: easy
5+
sidebar_label: Relative Ranks
6+
tags:
7+
- Array
8+
- Sorting
9+
- Java
10+
description: "This document provides solutions for the Relative Ranks problem."
11+
---
12+
13+
## Problem Statement
14+
15+
You are given an integer array `score` of size `n`, where `score[i]` is the score of the `i`-th athlete in a competition. All the scores are guaranteed to be unique.
16+
17+
The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:
18+
19+
- The 1st place athlete's rank is "Gold Medal".
20+
- The 2nd place athlete's rank is "Silver Medal".
21+
- The 3rd place athlete's rank is "Bronze Medal".
22+
- For the 4th place to the `n`-th place athlete, their rank is their placement number (i.e., the `x`-th place athlete's rank is "x").
23+
24+
Return an array `answer` of size `n` where `answer[i]` is the rank of the `i`-th athlete.
25+
26+
**Example 1:**
27+
28+
Input: `score = [5,4,3,2,1]`
29+
30+
Output: `["Gold Medal","Silver Medal","Bronze Medal","4","5"]`
31+
32+
Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].
33+
34+
**Example 2:**
35+
36+
Input: `score = [10,3,8,9,4]`
37+
38+
Output: `["Gold Medal","5","Bronze Medal","Silver Medal","4"]`
39+
40+
Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].
41+
42+
**Constraints:**
43+
44+
- `n == score.length`
45+
- `1 <= n <= 10^4`
46+
- `0 <= score[i] <= 10^6`
47+
- All the values in `score` are unique.
48+
49+
## Solutions
50+
51+
### Approach
52+
53+
To determine the ranks of the athletes based on their scores, we can follow these steps:
54+
55+
1. **Store Scores and Indices**:
56+
- Use a list to store the scores along with their original indices.
57+
58+
2. **Sort the List**:
59+
- Sort the list in descending order based on scores.
60+
61+
3. **Assign Ranks**:
62+
- Traverse through the sorted list and assign ranks accordingly:
63+
- "Gold Medal" for the 1st highest score.
64+
- "Silver Medal" for the 2nd highest score.
65+
- "Bronze Medal" for the 3rd highest score.
66+
- The placement number for the rest.
67+
68+
### Java
69+
70+
```java
71+
class Solution {
72+
public String[] findRelativeRanks(int[] score) {
73+
String[] ret = new String[score.length];
74+
ArrayList<Integer> list = new ArrayList<>();
75+
76+
for (int i = 0; i < score.length; i++) {
77+
list.add(score[i]);
78+
}
79+
80+
Collections.sort(list, Collections.reverseOrder());
81+
82+
for (int i = 0; i < score.length; i++) {
83+
int index = list.indexOf(score[i]) + 1;
84+
if (index == 1)
85+
ret[i] = "Gold Medal";
86+
else if (index == 2)
87+
ret[i] = "Silver Medal";
88+
else if (index == 3)
89+
ret[i] = "Bronze Medal";
90+
else
91+
ret[i] = index + "";
92+
}
93+
94+
return ret;
95+
}
96+
}
97+
```
98+
99+
### Python
100+
```Python
101+
class Solution:
102+
def findRelativeRanks(self, score: List[int]) -> List[str]:
103+
ret = [""] * len(score)
104+
sorted_scores = sorted(score, reverse=True)
105+
106+
for i in range(len(score)):
107+
rank = sorted_scores.index(score[i]) + 1
108+
if rank == 1:
109+
ret[i] = "Gold Medal"
110+
elif rank == 2:
111+
ret[i] = "Silver Medal"
112+
elif rank == 3:
113+
ret[i] = "Bronze Medal"
114+
else:
115+
ret[i] = str(rank)
116+
117+
return ret
118+
```
119+

0 commit comments

Comments
 (0)