Relative Ranks
EasyPrompt
You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.
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:
- The
1stplace athlete's rank is"Gold Medal". - The
2ndplace athlete's rank is"Silver Medal". - The
3rdplace athlete's rank is"Bronze Medal". - For the
4thplace to thenthplace athlete, their rank is their placement number (i.e., thexthplace athlete's rank is"x").
Return an array answer of size n where answer[i] is the rank of the ith athlete.
Example 1:
Input: score = [5,4,3,2,1]
Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].Example 2:
Input: score = [10,3,8,9,4]
Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].
Constraints:
n == score.length1 <= n <= 1040 <= score[i] <= 106- All the values in
scoreare unique.
Approaches
3 approaches with complexity analysis and trade-offs.
This is a straightforward brute-force approach. For each athlete, we iterate through all other athletes to count how many have a higher score. This count directly gives us the rank of the current athlete.
Algorithm
- Create a result array
answerof sizen. - Loop through each score
score[i]in the input array. - For each
score[i], initialize itsrankto 1. - Start a nested loop to compare
score[i]with every other scorescore[j]. - If
score[j]is greater thanscore[i], increment therank. - After the inner loop, the calculated
rankis the placement of the athlete. - Convert the rank number into the corresponding string ("Gold Medal", "Silver Medal", "Bronze Medal", or the number itself as a string).
- Store this rank string in
answer[i]. - Return the
answerarray after the outer loop finishes.
Walkthrough
The algorithm works by taking each score one by one and comparing it against every other score in the array. By counting how many scores are greater than the current score, we can determine its rank. For example, if zero scores are greater, the rank is 1. If one score is greater, the rank is 2, and so on. Once the numeric rank is found, it's converted to the required string format and placed in the result array at the same index as the original score.
class Solution { public String[] findRelativeRanks(int[] score) { int n = score.length; String[] answer = new String[n]; for (int i = 0; i < n; i++) { int rank = 1; for (int j = 0; j < n; j++) { if (score[j] > score[i]) { rank++; } } if (rank == 1) { answer[i] = "Gold Medal"; } else if (rank == 2) { answer[i] = "Silver Medal"; } else if (rank == 3) { answer[i] = "Bronze Medal"; } else { answer[i] = String.valueOf(rank); } } return answer; }}Complexity
Time
O(n^2), where n is the number of athletes. For each athlete, we perform a linear scan of the entire score array.
Space
O(n) to store the result array. If the output array is not considered, the space complexity is O(1).
Trade-offs
Pros
Simple to understand and implement.
Requires no extra space besides the output array.
Cons
Highly inefficient for larger inputs due to its quadratic time complexity.
Likely to cause a 'Time Limit Exceeded' error on most online judges for the given constraints.
Solutions
Solution
class Solution {public String[] findRelativeRanks(int[] score) { int n = score.length; Integer[] idx = new Integer[n]; for (int i = 0; i < n; ++i) { idx[i] = i; } Arrays.sort(idx, (i1, i2)->score[i2] - score[i1]); String[] ans = new String[n]; String[] top3 = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal"}; for (int i = 0; i < n; ++i) { ans[idx[i]] = i < 3 ? top3[i] : String.valueOf(i + 1); } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.