Algorithm


Problem Name: 506. Relative Ranks

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.

 

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 <= 104
  • 0 <= score[i] <= 106
  • All the values in score are unique.
 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public String[] findRelativeRanks(int[] nums) {
    PriorityQueue orderedRanking = new PriorityQueue<>((o1, o2) -> nums[o2] - nums[o1]);
    orderedRanking.addAll(IntStream.range(0, nums.length).boxed().collect(Collectors.toList()));
    String[] relativeRanks = new String[nums.length];
    int currentRank = 1;
    while (!orderedRanking.isEmpty()) {
      int removed = orderedRanking.poll();
      if (currentRank > 3) {
        relativeRanks[removed] = String.valueOf(currentRank++);
      } else {
        relativeRanks[removed] = (
            currentRank == 1 ? "Gold Medal" : (currentRank == 2 ? "Silver Medal" : "Bronze Medal")
        );
        currentRank++;
      }
    }
    return relativeRanks;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
score = [5,4,3,2,1]

Output

x
+
cmd
["Gold Medal","Silver Medal","Bronze Medal","4","5"]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const findRelativeRanks = function(nums) {
  const numIndexMapping = {}
  for (let index = 0; index  <  nums.length; ++index) {
    numIndexMapping[nums[index]] = index
  }
  let rank = nums.length
  for (let num in numIndexMapping) {
    const index = numIndexMapping[num]
    if (3 < rank) {
      nums[index] = '' + rank
    } else if (3 == rank) {
      nums[index] = 'Bronze Medal'
    } else if (2 == rank) {
      nums[index] = 'Silver Medal'
    } else {
      nums[index] = 'Gold Medal'
    }
    --rank
  }

  return nums
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
score = [5,4,3,2,1]

Output

x
+
cmd
["Gold Medal","Silver Medal","Bronze Medal","4","5"]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findRelativeRanks(self, nums):
        rank = {n:i>2 and str(i+1) or ["Gold","Silver","Bronze"][i] + ' Medal' for i,n in enumerate(sorted(nums,reverse=True))}
        return [rank[num] for num in nums]
Copy The Code & Try With Live Editor

Input

x
+
cmd
score = [10,3,8,9,4]

Output

x
+
cmd
["Gold Medal","5","Bronze Medal","Silver Medal","4"]

#4 Code Example with C# Programming

Code - C# Programming


using System.Linq;

namespace LeetCode
{
    public class _0506_RelativeRanks
    {
        public string[] FindRelativeRanks(int[] nums)
        {
            var max = nums.Max();
            var index = new int[max + 1];
            for (int i = 0; i  <  nums.Length; i++)
                index[nums[i]] = i + 1;

            var result = new string[nums.Length];
            var rank = 1;
            for (int i = max; i >= 0; i--)
            {
                if (index[i] != 0)
                {
                    if (rank == 1) result[index[i] - 1] = "Gold Medal";
                    else if (rank == 2) result[index[i] - 1] = "Silver Medal";
                    else if (rank == 3) result[index[i] - 1] = "Bronze Medal";
                    else result[index[i] - 1] = rank.ToString();

                    rank++;
                }
            }

            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
score = [10,3,8,9,4]

Output

x
+
cmd
["Gold Medal","5","Bronze Medal","Silver Medal","4"]
Advertisements

Demonstration


Previous
#504 Leetcode Base 7 Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#507 Leetcode Perfect Number Solution in C, C++, Java, JavaScript, Python, C# Leetcode