Skip to content

Latest commit

 

History

History
37 lines (28 loc) · 798 Bytes

506.md

File metadata and controls

37 lines (28 loc) · 798 Bytes

506 Relative Ranks

Description

link


Solution : HEAP

放入index即可,注意heap的组成内容


Code

Complexity T : O( nlog(n) )

class Solution:
    def findRelativeRanks(self, nums: List[int]) -> List[str]:
        heap = []
        for idx, x in enumerate(nums):
            heapq.heappush(heap, (x*-1, idx))
        result = [None]*len(nums)
        for i in range(len(nums)):
            a,b = heapq.heappop(heap)
            if i == 0:
                result[b] = "Gold Medal"
            elif i == 1:
                result[b] = "Silver Medal"
            elif i == 2:
                result[b] = "Bronze Medal"
            else:
                result[b] = str(i+1)
        return result