Review Note

Last Update: 07/03/2024 02:06 AM

Current Deck: LeetCode

Published

Fields:

Front
347. Top K Frequent Elements
  • Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Back
class Solution:    
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        res = []
        count = Counter(nums)
        freq = [[] for _ in range(len(nums) + 1)]
        for n, c in count.items():
            freq[c].append(n)
        for f in reversed(freq):
            for n in f:
                res.append(n)
                if len(res) == k:
                    return res

Tags:

Arrays_&_Hashing

Suggested Changes:

Deck Changes (Suggestion to move the Note to the following Deck):

Field Changes:

Tag Changes: