Skip to content

Commit 82cc632

Browse files
committed
Day 41: start with a copy of day 37 material
1 parent 42abb3b commit 82cc632

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

day41/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[Today's challenge is actually a follow-up on day37's challenge -- using a heap instead of radix sort]
2+
3+
Question of the day: https://leetcode.com/problems/top-k-frequent-elements/#/description
4+
5+
Given a non-empty array of integers, return the k most frequent elements.
6+
7+
For example,
8+
Given `[1,1,1,2,2,3]` and `k = 2`, return `[1,2]`.
9+
10+
Note:
11+
* You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
12+
* Your algorithm's time complexity must be better than O(n log n), where
13+
n is the array's size.
14+
15+
## Ideas
16+
17+
Can't do a normal sort, since that alone will take `O(nlogn)` runtime.
18+
The input array isn't sorted, so we need to keep track of a count and organize
19+
that count somehow as we iterate through the integers in the array. There
20+
doesn't seem to be any constraints on the types of integers on the array,
21+
so I'll assume that possible elements in the array range from -maxInt to maxInt
22+
.
23+
24+
I think I can actually use radix sort again. Same idea as the challenge from
25+
[Day 36](../day36).
26+
27+
## Code
28+
[Day 37 - Python](../day37/topKFrequent.py)
29+
30+
## Follow-up
31+
32+
Over the past few days ([38](../day38), [39](../day39), [40](../day40)), I got a
33+
little more familiar with the heap data structure and finally understand why
34+
heapifying an unsorted array can be done in linear time. The operations involved
35+
in heapify decrease exponentially over a logarithmic range, resulting in an overall
36+
linear amount of work. Anyways, I can use this `O(n)` time to heapify an unsorted
37+
array of frequencies, and then pop off the top `k` frequencies in `O(klogn)` time.
38+
The overall runtime would now be at most `O(nlogn)` if `k` == `n`. However, the
39+
real savings is in the `O(n)` space for storing all the elements of the heap.
40+
Much better than the `O(max value of the input array)` I had before.
41+
42+
## Code
43+
[Day 41 - Python](./topKFrequen.py)

day41/topKFrequent.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def topKFrequent(nums, k):
2+
3+
return ret
4+
5+
def testTopKFrequent():
6+
assert set(topKFrequent([], 0)) == set([])
7+
assert set(topKFrequent([1], 1)) == set([1])
8+
assert set(topKFrequent([-1, -1], 1)) == set([-1])
9+
assert set(topKFrequent([1,1,1,2,2,3], 2)) == set([1, 2])
10+
assert set(topKFrequent([-1,-1,-1,2,2,3], 2)) == set([-1, 2])
11+
assert set(topKFrequent([1,1,1,2,2,3], 3)) == set([1, 2, 3])
12+
assert set(topKFrequent([1,1,1,2,2,2,3,3,3], 3)) == set([1, 2, 3])
13+
assert set(topKFrequent([1, 2, 3, 4, 5], 1)) == set([1, 2, 3, 4, 5])
14+
assert set(topKFrequent([4,1,-1,2,-1,2,3], 2)) == set([-1, 2])
15+
assert set(topKFrequent([3,2,3,1,2,4,5,5,6,7,7,8,2,3,1,1,1,10,11,5,6,2,4,7,8,5,6], 10)) == set([1,2,5,3,7,6,4,8,10,11])
16+
17+
def main():
18+
testTopKFrequent()
19+
20+
if __name__ == "__main__":
21+
main()

0 commit comments

Comments
 (0)