347 Top K Frequent Elements
Published:
Problem
Solution
Intuition
The problem asks us to find the k
most frequent elements from a list that may contain repeats. In order to find the k
most frequent elements, we need to count the frequency of each number and then return the k
numbers with the highest frequency from the list.
Approach
- Initialize a map for $O(1)$ storing and look up of the frequencies of each number in the list.
- Iterate through the list and update the frequency of each number that shows up in the list.
- Initialize a list of lists to store all numbers that have the same frequency.
- Iterate through the map to store all numbers together that have the same frequency.
- Initialize a list for storing the result.
- Iterate through the frequency pairings list starting from the right where we have stored the most frequent elements, and continue adding elements until
k
elements are added to the result. - Return this result.
Complexity
- Time complexity: $O(n)$. We iterate through all numbers for counting frequencies, all numbers for pairing with equally frequent elements, and then
k
numbers for finding our result. $O(n)+O(n)+O(k) \rightarrow O(n)$ - Space complexity: $O(n)$. We store each element in two separate data structures and store the
k
most frequent elements. $O(n)+O(n)+O(k) \rightarrow O(n)$
Code
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
occurrences = defaultdict(int)
for num in nums:
occurrences[num] += 1
freq = [[] for i in range(len(nums) + 1)]
for num, count in occurrences.items():
freq[count].append(num)
res = []
for i in range(len(freq) - 1, -1, -1):
for j in freq[i]:
res.append(j)
if len(res) == k:
return res