Skip to content

Commit d61d2aa

Browse files
Chris WuChris Wu
authored andcommitted
top-k-frequent-elements.py
1 parent 6fbaf95 commit d61d2aa

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

house-robber-ii.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def rob(self, nums):
1414
if i==0:
1515
K[i] = nums[0]
1616
elif i==1:
17-
K[i] = max(nums[0], nums[1])
17+
K[i] = nums[0]
1818
elif i==len(nums)-1:
1919
K[i] = K[i-1]
2020
else:

top-k-frequent-elements.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
In both solution, the `counter` counts how many of each num in nums, as `counter[num]`=`num's count`
3+
The bottle neck is we need to get k nums which has largest count (in an efficient way).
4+
And here is how `Bucket Sort` and `Heap` comes in.
5+
6+
* Bucket Sort can sort things really quick, the trade off is we need to use lots of space.
7+
Now, because we know that the max possible count of each `num` is the count of `nums`.
8+
We can make an list (`bucket`), where index 0~`len(nums)`, representing the count.
9+
Every index `i` stores a list. In the list, all `num` has exactly i count.
10+
Next we can iterate backward, from large index to small index (from higher count to lower count), until we got k element in the output.
11+
12+
* Heap. We use the counter to build a max heap. Then we pop out the num which has higher count.
13+
14+
Time space analysis.
15+
* Bucket Sort. For time complexity, build the counter takes O(N), build the bucket also takes O(N).
16+
Getting the k num which has the highest count also takes O(N). So time complexity, is O(N).
17+
Space complexity is O(N). For buckets at most takes every element in `nums`.
18+
19+
* Heap. For time complexity, build the counter takes O(N), build the heap also takes O(N).
20+
(Yeah, I know. Some People thinks `heapify` takes O(NLogN) but it actually takes O(N). But that is another story...)
21+
Pop out from heap takes O(N) and we do that k times, kLogN.
22+
So time complexity, is O(N+kLogN).
23+
Space complexity is O(N).
24+
"""
25+
26+
27+
from collections import Counter, defaultdict
28+
import heapq
29+
30+
# bucket sort
31+
class Solution(object):
32+
def topKFrequent(self, nums, k):
33+
opt = []
34+
counter = collections.Counter(nums)
35+
bucket = collections.defaultdict(list)
36+
37+
for num, count in counter.items():
38+
bucket[count].append(num)
39+
40+
for i in reversed(xrange(len(nums)+1)):
41+
if i in bucket:
42+
opt.extend(bucket[i])
43+
if len(opt)>=k: break
44+
45+
return opt[:k]
46+
47+
# heap
48+
class Solution(object):
49+
def topKFrequent(self, nums, k):
50+
opt = []
51+
counter = collections.Counter(nums)
52+
53+
heap = [(-count, num) for num, count in counter.items()]
54+
heapq.heapify(heap)
55+
56+
while len(opt)<k:
57+
opt.append(heapq.heappop(heap)[1])
58+
59+
return opt
60+
61+
62+

0 commit comments

Comments
 (0)