[Leetcode] 347. Top K Frequent Elements

2021. 1. 26. 09:50Problem Solving/LeetCode

Leetcode 347. Top K Frequent Elements

https://leetcode.com/problems/top-k-frequent-elements/

 

Top K Frequent Elements - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Solution 1

collections.Counter() 이용한 풀이. most_common()메소드를 사용했다.

import collections
from typing import List

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        answer = []
        count = collections.Counter(nums)

        for tuple in count.most_common(k):
            answer.append(tuple[0])

        return answer

반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Leetcode 104. Maximum Depth of Binary Tree  (0) 2021.02.08
Leetcode 46. Permutations  (0) 2021.01.29
[Leetcode] 771. Jewels and Stones  (0) 2021.01.19
[Leetcode] 706. Design HashMap  (0) 2021.01.19
[Leetcode] 641. Design Circular Deque  (0) 2021.01.19