Top K Frequent Elements - LeetCode

 

<내 풀이>

from collections import Counter

class Solution:
    def topKFrequent(self, nums:List[int], k:int) -> List[int]:
        numDict = dict(Counter(nums))
        countlist = sorted(numDict.items(), reverse = True, key = lambda x: x[1])
        res=[]
        for i in range(k):
            res.append(countlist[i][0])
        return res

 

 

정렬, 딕셔너리로 풀기. 

 

key = lambda x: x[1] 이거는 왜 이렇게 안 익숙해질까.. 헣

+ Recent posts