506. Relative Ranks

506. Relative Ranks

8th May 2024 LeetCode Daily

ยท

3 min read

Problem Statement

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 1st place athlete's rank is "Gold Medal".

  • The 2nd place athlete's rank is "Silver Medal".

  • The 3rd place athlete's rank is "Bronze Medal".

  • For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is "x").

Return an array answer of size n where answer[i] is the rank of the ith athlete.

Examples

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.length

  • 1 <= n <= 10<sup>4</sup>

  • 0 <= score[i] <= 10<sup>6</sup>

  • All the values in score are unique.

Intuition

In this problem we need to return an array of string with rank of athletes in their original position as in score array. This is a classic problem in which the explanation of problem seems scary but the solution is very easy.

We need to tackle the following:

  • Keep the array in order to easily match ranks with positions: To achieve this, we can sort the array in descending order. Remember not to sort the original array but create a copy by using sorted() instead of List.sort().

  • Connect ranks and positions together: Once we sort the array, we can link the scores to ranks. For instance, if the array is:

    • scores: [3,5,6,2,9]

    • After sorting in descending order: scores_reversed: [9,6,5,3,2]

    • We can create a mapping like: {9:"Gold Medal",6:"Silver Medal",5:"Bronze Medal","4"}

  • Provide a new array with ranks in the original order of score array: Since we used the score as the key in the mapping, we can generate another array by looping through scores(the original array) and placing the value at that index.

Code Solution

Python Code:

# TC: O(n logn)
# SC: O(n)
class Solution:
    def findRelativeRanks(self, scores: List[int]) -> List[str]:
        # sort the score list in descending order
        # not sorting in place
        sorted_scores=sorted(scores,reverse=True)
        # kind of a map for Medals
        medals=["Gold Medal","Silver Medal","Bronze Medal"]
        # map score to the Medal or position
        score_map={score:medals[index] if index<3 else str(index+1) for index,score in enumerate(sorted_scores)}        
        # form a rank list according to original scores list and return it
        return [score_map[score] for score in scores]

Time and Space Complexity

Time Complexity:

$$O(n\ logn)$$

This is because we're sorting the array.

Space Complexity:

$$O(n)$$

As we're allocating extra space for the sorted_scores array.

Conclusion

Hope that explanation made sense and you found this blog helpful. If you've made it this far, please give it a like and drop a comment.

Happy Coding!๐Ÿš€

ย