506. Relative Ranks
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to determine the ranks of athletes based on their scores. We are given an array of unique scores, where the index of a score corresponds to a specific athlete. The ranking is based on the score value: the highest score gets 1st place, second highest gets 2nd, and so on. The top three ranks have special names ("Gold Medal", "Silver Medal", "Bronze Medal"), while subsequent ranks are represented by their placement number as a string (e.g., "4", "5"). The main challenge is to associate the calculated ranks back to the original athletes' indices.
Solution
Relative Ranks in Python
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
n = len(score)
# Create a list of tuples (score, original_index) using list comprehension.
score_pairs = [(score[i], i) for i in range(n)]
# Sort the list of tuples in descending order based on score.
score_pairs.sort(key=lambda x: x[0], reverse=True)
# Create a result list to store the ranks, initialized with empty strings.
result = [""] * n
# Define the top three rank strings.
medals = ["Gold Medal", "Silver Medal", "Bronze Medal"]
# Assign ranks based on the sorted order.
for i in range(n):
# The original index of the athlete.
original_index = score_pairs[i][1]
# The rank is i + 1.
rank = i
# Assign medals for top 3, and rank number for the rest.
if rank < 3:
result[original_index] = medals[rank]
else:
result[original_index] = str(rank + 1)
return result
Relative Ranks in Java
class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;
// Create a 2D array to store score and original index.
// pairs[i][0] = score, pairs[i][1] = index
int[][] pairs = new int[n][2];
for (int i = 0; i < n; i++) {
pairs[i][0] = score[i];
pairs[i][1] = i;
}
// Sort the pairs array in descending order based on score.
Arrays.sort(pairs, (a, b) -> b[0] - a[0]);
// Create a result array to store the ranks.
String[] result = new String[n];
// Assign ranks based on the sorted order.
for (int i = 0; i < n; i++) {
// The original index of the athlete.
int original_index = pairs[i][1];
// Assign medals for top 3, and rank number for the rest.
if (i == 0) {
result[original_index] = "Gold Medal";
} else if (i == 1) {
result[original_index] = "Silver Medal";
} else if (i == 2) {
result[original_index] = "Bronze Medal";
} else {
result[original_index] = String.valueOf(i + 1);
}
}
return result;
}
}
Relative Ranks in C++
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
int n = score.size();
// Create a vector of pairs to store score and original index.
vector<pair<int, int>> score_pairs;
for (int i = 0; i < n; ++i) {
score_pairs.push_back({score[i], i});
}
// Sort the pairs in descending order based on score.
// Sorting by the first element of the pair by default.
// Using rbegin() and rend() for reverse (descending) sorting.
sort(score_pairs.rbegin(), score_pairs.rend());
// Create a result vector to store the ranks.
vector<string> result(n);
// Assign ranks based on the sorted order.
for (int i = 0; i < n; ++i) {
// The original index of the athlete.
int original_index = score_pairs[i].second;
// Assign medals for top 3, and rank number for the rest.
if (i == 0) {
result[original_index] = "Gold Medal";
} else if (i == 1) {
result[original_index] = "Silver Medal";
} else if (i == 2) {
result[original_index] = "Bronze Medal";
} else {
result[original_index] = to_string(i + 1);
}
}
return result;
}
};
Relative Ranks in TypeScript
function findRelativeRanks(score: number[]): string[] {
const n = score.length;
// Create an array of pairs [score, original_index].
const pairs: [number, number][] = score.map((s, i) => [s, i]);
// Sort the pairs in descending order based on score.
pairs.sort((a, b) => b[0] - a[0]);
// Create a result array to store the ranks.
const result: string[] = new Array(n);
// Assign ranks based on the sorted order.
for (let i = 0; i < n; i++) {
// The original index of the athlete.
const original_index = pairs[i][1];
// The rank is i + 1.
const rank = i + 1;
// Assign medals for top 3, and rank number for the rest.
if (rank === 1) {
result[original_index] = "Gold Medal";
} else if (rank === 2) {
result[original_index] = "Silver Medal";
} else if (rank === 3) {
result[original_index] = "Bronze Medal";
} else {
result[original_index] = String(rank);
}
}
return result;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.