-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtop-k-frequent-elements.py
More file actions
29 lines (25 loc) · 1005 Bytes
/
top-k-frequent-elements.py
File metadata and controls
29 lines (25 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from collections import defaultdict
import heapq
from typing import List
class Solution:
# Time complexity: O(n log k)
# Space complexity: O(n)
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
hp = []
counter = defaultdict(int)
in_heap = set()
for num in nums:
counter[num] += 1
if hp and hp[0][1] == num:
heapq.heappushpop(hp, (counter[num], num))
elif len(hp) < k and num not in in_heap:
heapq.heappush(hp, (counter[num], num))
in_heap.add(num)
elif hp[0][0] < counter[num] and num not in in_heap:
while hp[0][0] < counter[hp[0][1]]:
heapq.heappushpop(hp, (counter[hp[0][1]], hp[0][1]))
if hp[0][0] < counter[num]:
in_heap.remove(hp[0][1])
in_heap.add(num)
heapq.heappushpop(hp, (counter[num], num))
return [y for x, y in hp]