Top K Frequent Elements | Leetcode

  Рет қаралды 64,244

Techdose

Techdose

3 жыл бұрын

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

Пікірлер: 62
@amanvijayvargiya3468
@amanvijayvargiya3468 3 жыл бұрын
Finally I am placed in delhivery company with decent package ...Thanks uuh so much .You inspired me a lot .I will always remember you.
@techdose4u
@techdose4u 3 жыл бұрын
Happy to hear this :) Wish you all the best ❤️
@amanvijayvargiya3468
@amanvijayvargiya3468 3 жыл бұрын
@@techdose4u thank you
@Onlyforyou_justjoking
@Onlyforyou_justjoking 10 ай бұрын
congratulations bro :)
@ankitbatra9832
@ankitbatra9832 3 жыл бұрын
Finally, got to see the man behind all these great videos! Thank you so much for contributing! :)
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@yitingg7942
@yitingg7942 3 жыл бұрын
And it's a handsome guy! 😋
@AlexZaharia24
@AlexZaharia24 2 жыл бұрын
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.
@neerajpandey7273
@neerajpandey7273 3 жыл бұрын
The explanation to choose between first (KlogN) and second(NlogK) was super.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@ShubhamKumar-lg2st
@ShubhamKumar-lg2st Ай бұрын
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.
@anikkhan8811
@anikkhan8811 3 жыл бұрын
Your Time Complexity analysis is top-notch!!! 😍😍 Plz continue explaining time complexity in your future videos too.
@techdose4u
@techdose4u 3 жыл бұрын
Improved after so many videos 😅
@anikkhan8811
@anikkhan8811 3 жыл бұрын
@@techdose4u Your experience is our quick lesson. Thanks for sharing 😀
@techdose4u
@techdose4u 3 жыл бұрын
@@anikkhan8811 😂 Lol. Thanks
@AryanSingh-zv5ch
@AryanSingh-zv5ch Жыл бұрын
@@techdose4u how come building a max heap is taking O(N) wouldn't it be O(N*logN)
@darshpreetnarula3034
@darshpreetnarula3034 Жыл бұрын
No youtube video has explained it better than you.Thanks!
@techdose4u
@techdose4u Жыл бұрын
Welcome :)
@rahulbera454
@rahulbera454 2 жыл бұрын
Thanks for the amazing explanation
@tulika2863
@tulika2863 3 жыл бұрын
If we use a max heap, will it not be 0(N + KlogN)? N for heapify and KLogN for popping K times.
@MalladiSankar
@MalladiSankar 2 жыл бұрын
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
@shruti4879
@shruti4879 2 жыл бұрын
What do you use for drawing on the canvas?
@hillarioushollywood4267
@hillarioushollywood4267 Жыл бұрын
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
@Hrit
@Hrit 2 жыл бұрын
Most detailed explanation
@priytoshtripathi1571
@priytoshtripathi1571 2 ай бұрын
A better solution would be to use Bucket sort technique, after counting the frequencies.We'll be able to solve it O(n)
@The_Shubham_Soni
@The_Shubham_Soni Жыл бұрын
Nice Explanation💯
@ommapari
@ommapari 2 жыл бұрын
I love you, sir, I became a fan of your explanations... I really want to meet you someday 😢😢
@xl3931
@xl3931 2 жыл бұрын
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)
@xl3931
@xl3931 2 жыл бұрын
without using vectors
@princeanthony8525
@princeanthony8525 2 жыл бұрын
The interviewer at Google expected a bucket sort solution in my case. He didn’t accept using a heap.
@shahenechaouachi4959
@shahenechaouachi4959 2 жыл бұрын
Damn did u get it? It's almost impossible to come up with bucket sort on the spot if u havent seen it before
@jaagrataggarwal4682
@jaagrataggarwal4682 2 жыл бұрын
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?
@RAVINDRAKUMAR-gj2nx
@RAVINDRAKUMAR-gj2nx 2 жыл бұрын
build a heap from an array take O(nlogn) time
@sharmabrothers3708
@sharmabrothers3708 3 жыл бұрын
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😍
@SiddharthKulkarniN
@SiddharthKulkarniN 7 ай бұрын
There is also a O(N) solution using bucket sort, isn't it?
@pottalokesh2039
@pottalokesh2039 3 жыл бұрын
Also can you explain something about that comparator function in next video ...
@techdose4u
@techdose4u 3 жыл бұрын
Sure :)
@prasannapm3220
@prasannapm3220 2 жыл бұрын
thank you
@Khabib901
@Khabib901 Жыл бұрын
thanks man
@nayanjain5761
@nayanjain5761 3 жыл бұрын
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.
@ApoorvaRajBhadani
@ApoorvaRajBhadani 2 жыл бұрын
yes same doubt
@shivamkeshri1131
@shivamkeshri1131 2 жыл бұрын
Build heap takes O(n) time
@dakshtaneja9189
@dakshtaneja9189 Жыл бұрын
yes, same doubt. each push operation will take log(n) and traversing through n elements would give us nlog(n).
@nirnitabiswas933
@nirnitabiswas933 6 ай бұрын
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) ??
@samareshms4591
@samareshms4591 2 жыл бұрын
After storing in unordered_map why cant we iterate and push those elements into vector which are >= k
@rishikantpatel5008
@rishikantpatel5008 2 жыл бұрын
Hello bhaiya but to build a heap of size it will take a nlogn time?
@parul15137
@parul15137 4 ай бұрын
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)
@nellylopez5152
@nellylopez5152 3 жыл бұрын
what does while(k - -) do ?
@techdose4u
@techdose4u 3 жыл бұрын
It will run until N becomes 0
@vasanthkumar5908
@vasanthkumar5908 Жыл бұрын
Build heap is N log N times because of heapify up
@MahirHasancse21
@MahirHasancse21 3 жыл бұрын
Why do you use a.freqb.freq?
@techdose4u
@techdose4u 3 жыл бұрын
Both are used for reverse order arrangement. Try it in your code. You will understand.
@srijandasbiswas4864
@srijandasbiswas4864 3 жыл бұрын
So comparator in priority queue is used for reverse order arrangement !!?? Please somebody confirm it !!
@ADNANAHMED-eo5xx
@ADNANAHMED-eo5xx 3 жыл бұрын
@@srijandasbiswas4864 yes plz
@seans9096
@seans9096 3 жыл бұрын
how come build heap is O(N)?
@techdose4u
@techdose4u 3 жыл бұрын
Watch my video on build heap algo
@gauravkapadia2514
@gauravkapadia2514 3 жыл бұрын
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.
@shivamkeshri1131
@shivamkeshri1131 2 жыл бұрын
How do you know the frequency w/o looking at the test cases?
@TheGameVentureYt
@TheGameVentureYt 2 жыл бұрын
This Question was asked by Facebook.
@techdose4u
@techdose4u 2 жыл бұрын
:)
@marin1419
@marin1419 10 ай бұрын
does amazon ask this?@@techdose4u
Sort K sorted array | Sort nearly sorted array
21:24
Techdose
Рет қаралды 20 М.
Sliding Window Maximum | Leetcode #239
25:38
Techdose
Рет қаралды 52 М.
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 32 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
00:56
صدام العزي
Рет қаралды 59 МЛН
🤔Какой Орган самый длинный ? #shorts
00:42
Задержи дыхание дольше всех!
00:42
Аришнев
Рет қаралды 1,7 МЛН
Top K Frequent Elements - Leetcode 347 - Heaps (Python)
14:08
Greg Hogg
Рет қаралды 4,5 М.
Top K Frequent Elements - Bucket Sort - Leetcode 347 - Python
13:13
Most Common Concepts for Coding Interviews
6:08
NeetCode
Рет қаралды 289 М.
Build Heap Algorithm | Proof of O(N) Time Complexity
15:32
Techdose
Рет қаралды 79 М.
Largest number formed from an array
8:33
Techdose
Рет қаралды 117 М.
Top K Frequent Words - Priority Queue Approach (LeetCode)
13:26
AlgosWithMichael
Рет қаралды 35 М.
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,4 МЛН
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 32 МЛН