Back to photostream

Relative Ranks Leetcode Solution

The problem Relative Ranks Leetcode Solution asks us to return a vector or an array of strings representing the relative ranks. We are provided with an array that represents the score obtained by athletes. Then we use the given score array to assign the ranks. There is a small change for the top 3 candidates. Instead of, assigning them simple numbers 1, 2, or 3. We need to assign Gold, Silver, and Bronze medal. Other than the top 3 candidates, we can assign them simple numbers from 4 to n. Let's take a look at a few examples.

 

Explanation: Since the given array represents the scores attained by the athletes. The athlete with the highest score will be given Gold Medal and so forth. Thus we gave Gold Medal to the athlete with a score of 3, Silver Medal to the athlete with a score of 2, and Bronze medal to the athlete with a score of 1.

 

Explanation: Since the top 3 candidates are awarded medals and the rest candidates are assigned a rank. Thus we have assigned medals to the top 3 candidates, and assigned rank 4, and 5 to the corresponding candidates.

Approach

The problem Relative Ranks Leetcode Solution asks us to assign the medals to the top 3 candidates and assign the ranks to the rest of the candidates. The simplest thing one can think of is to sort the given sequence. But we need to assign the ranks to the original indices. So, if we sort the given sequence directly, then we will miss the original indices. And will not be able to decide to assign the ranks.

 

www.tutorialcup.com/leetcode-solutions/relative-ranks-lee...

17 views
0 faves
0 comments
Uploaded on September 15, 2021