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

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

I hardly ever comment on YouTube, but I had to. Your second solution is unique compared to others I have seen online. Great work!

johnboamah
Автор

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.

Banana-Joanne
Автор

This is the best explanation I came across, thank you!

alibaba
Автор

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.

rahuldwivedi
Автор

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))

JoeTan-nqfq
Автор

at 9:22, why not use another hashmap instead of an array? we're linking particular key value pairs just like in the first hashmap we used to get the number of occurences no?

hatemlamine
Автор

The second solution is very creative, thanks for sharing!

christianjt
Автор

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?

jiasubaowenke
Автор

2:12, wait, it should've been O(klogn) if we only consider the work of heap

n.h.son
Автор

please help, how come the second solution is not o(n2)

samspeaks-hkvp
Автор

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)]

venzdrop