-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
97 lines (74 loc) · 1.96 KB
/
Solution.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import collections
import heapq
class datastream:
def __init__(self, k):
# num: frequency
self.numFreqMemo = collections.defaultdict(int)
self.numIndexMemo = collections.defaultdict(int)
self.k = k
self.data = []
def add(self, num):
# reset pq
if num in self.numIndexMemo:
index = self.numIndexMemo[num]
self.numFreqMemo[num] += 1
i = index - 1
while i >= 0 and self.numFreqMemo[self.data[i]] < self.numFreqMemo[num]:
# swap
a, b = self.data[i], self.data[index]
self.numIndexMemo[a], self.numIndexMemo[b] = self.numIndexMemo[b], self.numIndexMemo[a]
self.data[i], self.data[index] = b, a
i -= 1
else:
self.data.append(num)
self.numFreqMemo[num] = 1
self.numIndexMemo[num] = len(self.data) - 1
def get(self):
for num in self.data[:self.k]:
print(num, end=" ")
print('\n')
ds = datastream(k=3)
for num in [1,1,1,1,2,2,2,3,3,3,3,5,5]:
ds.add(num)
ds.get()
"""
nums = [1,1,1,1,2,2,2,3,3,3,3,5,5]
k = 2
output: [1,3]
Q:
1. Is list nums sorted? No
2. Will k > len(set(nums))?
assumption:
K > 0
General idea:
memo: {}
"""
# NlogK
def topKelementsOptimized(nums, k):
memo = collections.defaultdict(int)
for num in nums:
memo[num] += 1
pq = []
# NlogK
for num, freq in memo.items():
heapq.heappush(pq, (freq, num))
if len(pq) > k:
heapq.heappop(pq)
res = []
while pq:
_, num = heapq.heappop(pq)
res.append(num)
return res
# NlogN
def topKelementsOptimizedNlogN(nums, k):
memo = collections.Counter(nums)
pq = []
# NlogN
for num, freq in memo.items():
heapq.heappush(pq, (-freq, num))
# KlogN
res = []
for _ in range(k):
_, num = heapq.heappop(pq)
res.append(num)
return res