You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Lists/top_k_frequent_elements.py
+49-10Lines changed: 49 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
Top K Frequent Elements
3
3
4
4
Given a non-empty array of integers, return the k most frequent elements.
5
+
The order of the result isn't important.
5
6
6
7
Input: [1, 1, 1, 2, 2, 3], 2
7
8
Output: [1, 2]
@@ -10,12 +11,13 @@
10
11
Output: [1]
11
12
12
13
=========================================
13
-
Using Priority Queue, add each frequency and remove the last if there are more than K elements inside the Priority Queue.
14
-
Time Complexity: O(N LogK)
14
+
Using Min Priority Queue, in each step add an element with its frequency and remove the element with the smallest frequency
15
+
if there are more than K elements inside the Priority Queue. This solution isn't much faster than sorting the frequencies.
16
+
Time Complexity: O(U LogK) , U in this case is the number of unique elements (but all elements from the array could be unique, so because of that U can be equal to N)
15
17
Space Complexity: O(N)
16
18
Using pivoting, this solution is based on the quick sort algorithm (divide and conquer).
17
-
Same pivoting solution as the nth_smallest.py,
18
-
Time Complexity: O(N)
19
+
Same pivoting solution as the nth_smallest.py.
20
+
Time Complexity: O(U)
19
21
Space Complexity: O(N)
20
22
'''
21
23
@@ -24,10 +26,37 @@
24
26
# Solution 1 #
25
27
##############
26
28
29
+
importheapq
30
+
31
+
# priority queue comparator class
32
+
# acctualy in this case you don't need a comparator class, because the elements are tuples
33
+
# and comparison operator can work with tuples
34
+
# (The comparison starts with a first element of each tuple. If they do not compare to =,< or > then it proceed to the second element and so on.)
35
+
classPQElement:
36
+
def__init__(self, el):
37
+
self.frequency, self.val=el
38
+
39
+
def__lt__(self, other):
40
+
returnself.frequency<other.frequency
41
+
42
+
# priority queue
43
+
classPriorityQueue:
44
+
def__init__(self):
45
+
self.data= []
46
+
47
+
defpush(self, el):
48
+
heapq.heappush(self.data, PQElement(el))
49
+
50
+
defpop(self):
51
+
returnheapq.heappop(self.data)
52
+
53
+
defcount(self):
54
+
returnlen(self.data)
55
+
27
56
deftop_k_frequent_1(nums, k):
28
57
frequency= {}
29
58
30
-
# count the frequency of each element
59
+
# count the frequency of each unique element
31
60
fornuminnums:
32
61
ifnuminfrequency:
33
62
frequency[num] +=1
@@ -42,7 +71,17 @@ def top_k_frequent_1(nums, k):
42
71
ifk<1:
43
72
return []
44
73
45
-
# TODO: Implement Priority Queue and solve it
74
+
heap=PriorityQueue()
75
+
forelinarr:
76
+
# push all elements
77
+
heap.push(el)
78
+
# pop the element with smallest frequency
79
+
ifheap.count() >k:
80
+
heap.pop()
81
+
82
+
# take all elements from the heap
83
+
# no need from K times heap.pop() (K LogK), because we already have the array with data (K)
84
+
return [el.valforelinheap.data]
46
85
47
86
48
87
##############
@@ -52,7 +91,7 @@ def top_k_frequent_1(nums, k):
52
91
deftop_k_frequent_2(nums, k):
53
92
frequency= {}
54
93
55
-
# count the frequency of each element
94
+
# count the frequency of each unique element
56
95
fornuminnums:
57
96
ifnuminfrequency:
58
97
frequency[num] +=1
@@ -67,7 +106,7 @@ def top_k_frequent_2(nums, k):
67
106
ifk<1:
68
107
return []
69
108
70
-
# pivoting, find the first k element with the biggest frequency, O(U), U = num of unique nums
109
+
# pivoting, find the first k elements with the biggest frequency, O(U), U = num of unique nums
0 commit comments