Simple Python Solution


  • 0
    A

    Sort the enumerated list (to give index values) in reverse order. The first 3 positions of this list will be Gold, Silver and Bronze respectively.

    class Solution(object):
        def findRelativeRanks(self, nums):
            medals = ['Gold Medal', 'Silver Medal', 'Bronze Medal']
            rank = sorted(list(enumerate(nums)), 
                          key=lambda x: x[1], 
                          reverse=True)
            for i, k in enumerate(rank):
                nums[k[0]] = str(i + 1) if i > 2 else medals[i]
            return nums
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.