放入index即可,注意heap的组成内容
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