1 of 3
Write a function top_k_frequent(nums, k) that returns the k most frequent elements from a list of integers.
You may return the answer in any order.
print(top_k_frequent([1,1,1,2,2,3], 2)) # Output: [1, 2] print(top_k_frequent([1], 1)) # Output: [1] print(top_k_frequent([4,4,4,6,6,2,2,2,2], 2)) # Output: [2, 4]
| Complexity | |
|---|---|
| Time | O(n log k) |
| Space | O(n) |
Where n is the length of the input list and k is the number of top elements.
Why O(n log k)? Counting frequencies takes O(n). Then maintaining a heap of size k means each push/pop is O(log k), and we do this for each unique element.
(frequency, element) pair, and pop when size exceeds kimport heapq from collections import Counter def top_k_frequent(nums, k): # Your code here pass # Test your function: print(top_k_frequent([1,1,1,2,2,3], 2)) # Output: [1, 2] print(top_k_frequent([1], 1)) # Output: [1] print(top_k_frequent([4,4,4,6,6,2,2,2,2], 2)) # Output: [2, 4]
Use Counter(nums) to build the frequency map. Then you need to find the k keys with the highest counts.
A min-heap of size k works well here:
(count, num) onto the heapk, pop the smallest β this removes the least frequent among the current top candidatesk frequent elementsheapq.nlargest(k, counts.keys(), key=counts.get) does the same thing in one line!
import heapq from collections import Counter def top_k_frequent(nums, k): counts = Counter(nums) heap = [] for num, freq in counts.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap]
Why this works: We maintain a min-heap of size k. When we push a new element and the heap exceeds size k, we pop the smallest frequency. After processing all elements, only the k most frequent remain.
Time: O(n log k) β n for counting, then at most n push/pop operations each costing O(log k).
Space: O(n) β for the frequency map.
import heapq from collections import Counter def top_k_frequent(nums, k): counts = Counter(nums) return heapq.nlargest(k, counts.keys(), key=counts.get)
Simpler, same time complexity internally.
What if you needed to return the k least frequent elements instead? Modify your solution!
print(top_k_least_frequent([1,1,1,2,2,3], 2)) # Output: [2, 3] (frequency 2 and 1 β the two least frequent)
β Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect
β Functions, classes, recursion, print()
β No file system, subprocess, OS access, or network requests
β No pip install (all supported modules are pre-loaded)
β±οΈ 5 second execution time limit