Master Data Structures & Algorithms For FREE at AlgoMap.io!
@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-Joanne5 күн бұрын
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.
@alibaba8882 ай бұрын
This is the best explanation I came across, thank you!
@JoeTan-nq4fq2 ай бұрын
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Ай бұрын
I'm not sure you'd be allowed to use most_common in an interview 🤔
@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.
@jiasubaowenke5 ай бұрын
Hi, could you please explain why it's O(nlogk) instead of O(klogn)? I don't know why, thank you!
@AryanShetty-d6b28 күн бұрын
@@jiasubaowenke becuse min heap will contain only k times elemnt others will e poped out
@venzdrop11 күн бұрын
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)]
@christianjt70185 ай бұрын
The second solution is very creative, thanks for sharing!
@GregHogg5 ай бұрын
I love these heap questions, there's some really interesting solutions
@Flybot20 Жыл бұрын
Good work. Keep going 👍🏼
@jiasubaowenke5 ай бұрын
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 Жыл бұрын
Fantastic! Thank you for that !
@GregHogg Жыл бұрын
Very welcome :)
@samspeaks-hk1vp4 ай бұрын
please help , how come the second solution is not o(n2)
@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).
@fadygamilmahrousmasoud58635 ай бұрын
This 2nd solution is briliant
@GregHogg5 ай бұрын
It's really cool
@n.h.son19027 ай бұрын
2:12, wait, it should've been O(klogn) if we only consider the work of heap
@samspeaks-hk1vp5 ай бұрын
how its o(n) ?we have inner loop
@guinea_horn5 ай бұрын
Because there isn't an inner loop?
@samspeaks-hk1vp4 ай бұрын
@@guinea_horn second solution from line 15 to 19 should not it be o(n2) ?
@guinea_horn4 ай бұрын
@@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