 What's up guys my is Michael and welcome to my YouTube channel today. We're gonna do another leak code heap question This is called top K frequent elements Given in a non-empty array of integers return the K most Frequent elements, so this is actually the exactly the same thing as the kth largest element in an array Only difference is that we're going to have to instead of just the kth largest element array it's going to be we have to Keep track of the occurrences for each value in this array And then we use the same algorithm that we use for kth largest element in array based on the occurrences that we have in the array Okay So I'm gonna start out this problem by just creating a map that holds all the occurrences of our For each element Inside nums, so I'm gonna go through all my nums Every value actually you know what I'm not gonna do this indexing You could just do this for each value inside nums Okay, it's much faster And what are we gonna? Oh, whoops For each value inside nums. We're gonna do occurrence at value Plus plus so that's gonna keep the frequency keep track how many times each the value in each num Value each value occurs inside nums Now we're gonna use the same algorithm that we use for a top For a kth largest element in array so instead of actually building the for the heap Instead of creating a Max heap so normally you would use a max heap if you want the most frequent element because of the the first the largest values are in the beginning and then you could just Remove k times from the beginning of your heap, but we're not gonna do that because that takes a Size of n to build our heap and then k times to remove Iterations, so we're actually gonna use a min heap and then we're gonna create a min heap of size k and if whatever value we're going through is less than the is greater than The min heap we're gonna remove the value of our min heap and add it to there. So that's what we're gonna do Okay, so once I have the occurrences though Here's what I have to do. I have to add each occurrence to I have to pair up the occurrence with the value In our min heap In our weight, let's see in our yeah in our main heap, so I'm gonna have to do a for every for every let's see Auto each valve Inside our currents so every for every maybe not each valve each frequency. Yeah for each frequency inside our currents I need to maintain a min heap So my priority queue it's gonna be integer It's gonna be a pair because I'm pairing up each integer with it's the number of occurrences, right so Yeah, a vector int pair int int and then Because it's a min heap. We have to use Greater yeah greater pair int and Yeah, that's how it would look like then Did I do that? Yeah, that's it mean heat Okay so then what I'm going to do is if You if each frequency if our main heap That size is less than k. I'm going to add the values of each frequency into our main heap So I'm gonna do main heap dot push Each frequency So we paired up each value with its number of occurrences So when I if I want to sort based on the number of occurrences occurrences I'm gonna do each frequency dot second and then each frequency Dot first because that's how The comparison works it always compares the first value in your pair And because of that we have to change the order of these when we add it. Yeah Because I want to sort by frequencies first then the number. Yeah, not the not the other way around Yeah, I'm gonna do that Push this otherwise I have to check the top value so I'm gonna I'm gonna make it split the pair into two two parts because Just in case of this is confusing for you guys. So each frequency so The top pair is Min heap dot Dot top and this is a pair So how we made the main heap is that our main heap is gonna be the first pair is gonna be a number of occurrences Then it's our actual number Right because we want to sort it by number of occurrences and then our number We don't want it to be number than number of occurrences that just doesn't wouldn't make any sense, right? That would just be exactly the same as our map so, yeah So I'm gonna get the occurrences Occurrences num occurrences, I'll do that. No occurrences That's the first part dot first and then The real number the actual number Okay, so what I'm gonna do is if num occurrences so if each frequency dot second is Yes, so the reason why we're doing second is because we mapped each number to number of occurrences for our map right and then for our main heap we Sorted by the number of occurrences and then the we paired up the number of occurrences and then the actual number in our main heap So like we changed the order of those two Because our main heap need we need to main heap the comparator compares the first value So that's why we change the order. I don't think you could do you could like read your own comparator So then you don't have to change the order of it, but I decided to change the order of it. So if If each frequency that second is greater than our number of occurrences At our top of our main heap. So this is at the top of our main heap we're going to break not break we're going to pop and then Add this again, so we're going to do in heap dot When he popped and then we're going to copy this Added to our main heap. I don't know what my computer's lagging. But yeah, that's what we're going to do and then at the end We have to return our main heap. So let's just get rid of this So the end of our for loop here So we need to return the case most frequent elements and that's just going to be To return that created to return I did not create a return vector int to return so While our heap is Not empty That's not empty I'm going to push Min heap dot second Dot top dot second the reason why I'm doing dot second is because the second value How we ordered it was the actual number and then we paired the first value was the the number of occurrences Right, so that second for a main heap is the actual number So push dot second. I don't know why this came up Then at the end, we're just going to do return to return. That's basically it Submit to push back. We got to do push back and it got accepted So yeah, that's basically how you do it Rate com subscribe. I'll check you guys later. Uh, wait, why is it only 41% faster? It has to be other one we did exactly the same way. Yeah, okay Um, maybe if you get rid of the these two We could get rid of this So we could get rid of the variable and submit again see Okay, it's still 41% but we use less memory ideas that we use the last memory. Um So why why is it only 41% is it because of the auto? I don't know Anyway, that's how you do this question very calm subscribe. I'll check you guys later. Peace