Top K Frequent Elements - Leetcode 347 - Heaps (Python)

  Рет қаралды 15,219

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 30
@GregHogg
@GregHogg 6 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@johnboamah9510
@johnboamah9510 Ай бұрын
I hardly ever comment on KZbin, but I had to. Your second solution is unique compared to others I have seen online. Great work!
@Banana-Joanne
@Banana-Joanne 5 күн бұрын
very grateful for your solution. admittedly, I'm not too familiar with heaps and bucket sort, so they both really went over my head. but I will keep on researching. Thanks for the good work.
@alibaba888
@alibaba888 2 ай бұрын
This is the best explanation I came across, thank you!
@JoeTan-nq4fq
@JoeTan-nq4fq 2 ай бұрын
Since we are already using Counter class, we can use most_common() method. counter = Counter(nums) top_k = counter.most_common(k) return list(map(lambda x: x[0], top_k))
@0xDomain
@0xDomain Ай бұрын
I'm not sure you'd be allowed to use most_common in an interview 🤔
@rahuldwivedi4758
@rahuldwivedi4758 Жыл бұрын
Great work. But if you switch context in the middle, please make sure to complete the context. At 4:32 you were talking about max_heap and in that process it seemed you were explaining the time complexity would be O(Nlogk) but it actually was for min_heap.
@jiasubaowenke
@jiasubaowenke 5 ай бұрын
Hi, could you please explain why it's O(nlogk) instead of O(klogn)? I don't know why, thank you!
@AryanShetty-d6b
@AryanShetty-d6b 28 күн бұрын
@@jiasubaowenke becuse min heap will contain only k times elemnt others will e poped out
@venzdrop
@venzdrop 11 күн бұрын
from collections import Counter class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: f = Counter(nums) return [x[0] for x in f.most_common(k)]
@christianjt7018
@christianjt7018 5 ай бұрын
The second solution is very creative, thanks for sharing!
@GregHogg
@GregHogg 5 ай бұрын
I love these heap questions, there's some really interesting solutions
@Flybot20
@Flybot20 Жыл бұрын
Good work. Keep going 👍🏼
@jiasubaowenke
@jiasubaowenke 5 ай бұрын
Thank you so much! I have a question that why don't we just use couter.most_common in the heap solution, is it slower than the heap sort?
@LearningWorldChatGPT
@LearningWorldChatGPT Жыл бұрын
Fantastic! Thank you for that !
@GregHogg
@GregHogg Жыл бұрын
Very welcome :)
@samspeaks-hk1vp
@samspeaks-hk1vp 4 ай бұрын
please help , how come the second solution is not o(n2)
@johnboamah9510
@johnboamah9510 Ай бұрын
The loop that fills the bucket array runs in O(n), where n is the length of nums. The loop that retrieves the top k elements iterates over the bucket array (size of n + 1) with O(n). Both loops are O(n), so the overall time complexity is O(n) not o(n^2).
@fadygamilmahrousmasoud5863
@fadygamilmahrousmasoud5863 5 ай бұрын
This 2nd solution is briliant
@GregHogg
@GregHogg 5 ай бұрын
It's really cool
@n.h.son1902
@n.h.son1902 7 ай бұрын
2:12, wait, it should've been O(klogn) if we only consider the work of heap
@samspeaks-hk1vp
@samspeaks-hk1vp 5 ай бұрын
how its o(n) ?we have inner loop
@guinea_horn
@guinea_horn 5 ай бұрын
Because there isn't an inner loop?
@samspeaks-hk1vp
@samspeaks-hk1vp 4 ай бұрын
@@guinea_horn second solution from line 15 to 19 should not it be o(n2) ?
@guinea_horn
@guinea_horn 4 ай бұрын
​@@samspeaks-hk1vpno, and I'm not sure why you think it is. Unless by n2 you mean 2*n rather than n^2, because we do have two iterations over n items but not an n^2 loop. 2*n reduces to O(n) in typical big o notation
@richardpro8927
@richardpro8927 Жыл бұрын
We need more
@cmarquay
@cmarquay Жыл бұрын
heappy heap
@GregHogg
@GregHogg Жыл бұрын
Heap it up
@rattlik1
@rattlik1 4 ай бұрын
most_common?
K Closest Points to Origin - Leetcode 973 - Heaps (Python)
9:47
Kth Largest Element in an Array - Leetcode 215 - Heaps (Python)
11:52
I'VE MADE A CUTE FLYING LOLLIPOP FOR MY KID #SHORTS
0:48
A Plus School
Рет қаралды 20 МЛН
Хаги Ваги говорит разными голосами
0:22
Фани Хани
Рет қаралды 2,2 МЛН
Top K Frequent Elements - Bucket Sort - Leetcode 347 - Python
13:13
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 15 М.
TOP K FREQUENT WORDS| LEETCODE 692 | PYTHON CUSTOM HEAP SOLUTION
13:38
Cracking FAANG
Рет қаралды 3,6 М.
Top K Frequent Elements | Leetcode #347 | Heap | Hashmap
18:25
3Sum (Updated Solution) - Leetcode 15 - Two Pointers (Python)
11:11
Koko Eating Bananas - Leetcode 875 - Binary Search (Python)
13:34
Coin Change - Leetcode 322 - Dynamic Programming (Python)
15:27