DEV Community

Karleb
Karleb

Posted on

#506. Relative Ranks

https://leetcode.com/problems/relative-ranks/?envType=daily-question&envId=2024-05-08


/**
 * @param {number[]} score
 * @return {string[]}
 */
var findRelativeRanks = function (score) {
    const copy = [...score].sort((a, b) => b - a)
    const ranks = ["Gold Medal", "Silver Medal", "Bronze Medal"]
    const result = new Array(score.length)

    for (let i in score) {
       let index = copy.indexOf(score[i]) 

       if (index < 3) {
        result[i] = ranks[index]
       } else {
        result[i] = (index + 1).toString()
       }
    }

    return result
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)