-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_k_frequent.py
More file actions
28 lines (22 loc) · 977 Bytes
/
top_k_frequent.py
File metadata and controls
28 lines (22 loc) · 977 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
# Given an integer array nums and an integer k, return the k most frequent elements within the array.
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
count = {}
freqBucket = [[] for i in range(len(nums) + 1)]
# count number of elements into a hash map
for n in nums:
count[n] = 1 + count.get(n, 0)
# iterate through kvp to create a bucket sorted list where index is the number and value is frequency
for n, c in count.items():
freqBucket[c].append(n)
result = []
# loop through frequency bucket backwards
for i in range(len(freqBucket) - 1, 0, -1):
# append frequency to results array
for n in freqBucket[i]:
result.append(n)
# once results reaches length of k return result
if len(result) == k:
return result
# O(n) + O(n)
# bucket sort