Top K Frequent Elements | Leetcode #347 | Heap | Hashmap

preview_player
Показать описание
This video explains a very important programming interview problem which is to find the top k frequent elements in a given array.This problem is based on heap and hashmap.I have shown all the required concepts by showing the intuition using examples.I have first explained the simple solution and then optimized to give a better solution.I have explained the problem in two different ways using heap and hash and i have also compared the efficiency with each other and showed which is better and why.

CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

==========================================PLEASE DONATE=============================
==============================================================================

=======================================================================
USEFUL LINKS:

RELATED LINKS:

#heap #techdose #heapcourse
Рекомендации по теме
Комментарии
Автор

Finally I am placed in delhivery company with decent package ...Thanks uuh so much .You inspired me a lot .I will always remember you.

amanvijayvargiya
Автор

Finally, got to see the man behind all these great videos! Thank you so much for contributing! :)

ankitbatra
Автор

The explanation to choose between first (KlogN) and second(NlogK) was super.

neerajpandey
Автор

Your Time Complexity analysis is top-notch!!! 😍😍 Plz continue explaining time complexity in your future videos too.

anikkhan
Автор

No youtube video
has explained it better than you.Thanks!

darshpreetnarula
Автор

You assume that adding elements to the heap is O(K) or O(N) which in fact will be O(KlogK) and O(NlogN) respectively. That means that the second heap algo is better than the first one.

AlexZaharia
Автор

Why we need to sort the hashmap? in 5.30min, instead will loop through each element in map and compare the val with k. if val is grater then equal to k then add that element to list and return, the time comlexity will be O(2n)=> O(n)

jayaprakashch
Автор

I love you, sir, I became a fan of your explanations... I really want to meet you someday 😢😢

ommapari
Автор

Sir, please correct me if I am wrong, for build heap (line 26-29) it's going to take O(nLog(n)) time as for all n elements you are pushing into a queue (pushing is a Log(n) operation). So O(nLog(K)) will be best approach to use here.

nayanjain
Автор

The question arises that how to make heapify based on values and not on keys? Because, The heapify() on tuples considers the first element in the tuple for the process

hillarioushollywood
Автор

If we use a max heap, will it not be 0(N + KlogN)? N for heapify and KLogN for popping K times.

tulika
Автор

I think we should use minheap isn't it? we need to pop from the root all the smaller values(frequencies with small values first) and add high frequency ones to the heap

MalladiSankar
Автор

A better solution would be to use Bucket sort technique, after counting the frequencies.We'll be able to solve it O(n)

priytoshtripathi
Автор

Why do you use a.freq<b.freq instead of using a.freq>b.freq?

MahirHasancse
Автор

will heap not take O(nlogn) for insertion? You said to insert n elements, heap would take O(n).But for rearranging after every insertion it takes logn time. So would not it take O(nlogn) time for inserting n elements?

jaagrataggarwal
Автор

Also can you explain something about that comparator function in next video ...

pottalokesh
Автор

What do you use for drawing on the canvas?

shruti
Автор

The interviewer at Google expected a bucket sort solution in my case. He didn’t accept using a heap.

princeanthony
Автор

Build heap is N log N times because of heapify up

vasanthkumar
Автор

There is also a O(N) solution using bucket sort, isn't it?

SiddharthKulkarniN