Top K Frequent Elements | Leetcode
Вставка
- Опубліковано 27 січ 2021
- 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=============================
🧡 SUPPORT OUR WORK: / techdose
💚 UPI-ID: surya.kahar@ybl
💞JOIN Membership: / @techdose4u
==============================================================================
INSTAGRAM : / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
TELEGRAM Channel LINK: t.me/codewithTECHDOSE
TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
=======================================================================
USEFUL LINKS:
🟠Must do TIPS to ACE Virtual Interview: • 🔴Must do Tips to ACE y...
🟢Best strategy to excel your coding interview: • 🔴Best strategy to exce...
🟡Get your dream job in 1 month: • 🔴Get your dream job in...
🔵How to crack dream job in just 2 months: • How to crack dream job...
🟣7 Days DSA plan: techdose.co.in/7-days-dsa-che...
RELATED LINKS:
Power of Heap: • Power of Heap
Concepts of Heap: • Concepts of Heap | Und...
Representation of Heap: • Representation of Heap...
Heapify Algorithm: • Heapify Algorithm | Ma...
Build heap algorithm: • Build Heap Algorithm |...
Heap Algorithm: • Heap Algorithms | Extr...
Heapsort Algorithm: • Heapsort Algorithm | C...
Heap Implementation: • Heap Implementation | ...
CODE LINK: techdose.co.in/top-k-frequent...
#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.
Happy to hear this :) Wish you all the best ❤️
@@techdose4u thank you
congratulations bro :)
Finally, got to see the man behind all these great videos! Thank you so much for contributing! :)
Welcome :)
And it's a handsome guy! 😋
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.
The explanation to choose between first (KlogN) and second(NlogK) was super.
Thanks :)
he has assumed to add 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.
Your Time Complexity analysis is top-notch!!! 😍😍 Plz continue explaining time complexity in your future videos too.
Improved after so many videos 😅
@@techdose4u Your experience is our quick lesson. Thanks for sharing 😀
@@anikkhan8811 😂 Lol. Thanks
@@techdose4u how come building a max heap is taking O(N) wouldn't it be O(N*logN)
If we use a max heap, will it not be 0(N + KlogN)? N for heapify and KLogN for popping K times.
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
No youtube video
has explained it better than you.Thanks!
Welcome :)
What do you use for drawing on the canvas?
Thanks for the amazing explanation
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
I love you, sir, I became a fan of your explanations... I really want to meet you someday 😢😢
Most detailed explanation
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?
build a heap from an array take O(nlogn) time
Also can you explain something about that comparator function in next video ...
Sure :)
Sir how to implement that last approach because after making how we remember which one is pop... Because poping only from top... Please sir give light on it.
Ret your videos is awesome sir😍
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.
yes same doubt
Build heap takes O(n) time
yes, same doubt. each push operation will take log(n) and traversing through n elements would give us nlog(n).
same doubt @tech-dose, from your prev videos i know build heap takes o(n) time, but pushing operation of STL PQ takes log n as it sorts also after pushing, it does that for n elements do shouldn't it be O(nlogn) ??
how can I implement this solution in terms of stop words. I need to get input from a txt file and store them in data structure after that while getting top 10 frequent element of txt file, I should not count stop words. ( there is file called stopwords.txt)
without using vectors
Nice Explanation💯
After storing in unordered_map why cant we iterate and push those elements into vector which are >= k
The interviewer at Google expected a bucket sort solution in my case. He didn’t accept using a heap.
Damn did u get it? It's almost impossible to come up with bucket sort on the spot if u havent seen it before
There is also a O(N) solution using bucket sort, isn't it?
A better solution would be to use Bucket sort technique, after counting the frequencies.We'll be able to solve it O(n)
thank you
Why do you use a.freqb.freq?
Both are used for reverse order arrangement. Try it in your code. You will understand.
So comparator in priority queue is used for reverse order arrangement !!??
Please somebody confirm it !!
@@srijandasbiswas4864 yes plz
thanks man
what does while(k - -) do ?
It will run until N becomes 0
Hello bhaiya but to build a heap of size it will take a nlogn time?
u can push all the values in a vector first and then use build heap on it so the time taken wil be O(2n)
how come build heap is O(N)?
Watch my video on build heap algo
Build heap is N log N times because of heapify up
why do we need sorting after hash map? we can just traverse one time for hash map values and filter all values whose frequency is >= k and append key to result list.
How do you know the frequency w/o looking at the test cases?
This Question was asked by Facebook.
:)
does amazon ask this?@@techdose4u