 What's up guys my name is Michael and welcome to my YouTube channel today We're gonna do another lead code challenge called top K frequent words. This is basically the same thing as doing Top K frequent elements Except they want you to sort it the based on the word with the lower alphabet comes first So given a list of words return the kth most frequent elements But your answer has to be sorted from frequency from highest lowest and if two words have the same frequency then The word with the lower alphabet order comes first. So in this case, I love leak code I love leak I love coding see I occurs twice Love occurs twice, right? And when they say k equal to 2 they want the two most frequent elements because I and love have the same number of occurrences frequencies occurring inside the the array I And love are the gonna be put into the output But because they have the same number exactly the same frequency of their words We have to do the one with the lower alphabet comes first. So I will come first then love will come first Okay, and it's the same thing for this. So if you guys already watched my previous video of Top K frequent elements you could click that video down there I don't maybe I'll put a card up just in case if you guys haven't watched it before But yeah, it's this is exactly the same code as that. So first of all, we have to have a map To count the number of occurrences, so I'm gonna do let's see its string and then I'm gonna do occurrences Okay, and this we're gonna call this occurrences I'll do a curve for short. Okay. I'm gonna go through for every string. Okay. I'm gonna do auto Hmm, okay, I'll do it for every string X Inside words so forever you for every For every string inside words, we're gonna do occurrence at X plus plus and that's gonna give us occurrences of every single word Okay, I'm instead of X. I'll do like word because forever you word inside words. Okay, just to make it more readable Okay, once I get there a number of occurrences, I need to heap I need a heap to keep track of the ones with the highest frequency and then to the lowest So they want us to basically Sort frequency by highest the lowest. So if I were to go through a Hi, okay, so if I were to go through the whole the whole array and Generate a max heap and add all the values there and then take the first K elements That could work, but I don't want to do that right if I were to go through all the whole the whole array and Create a max heap and generate all the Get all the occurrences it will sort the because it's a heap It will sort the highest first and then the lowest because it's a max heap But I don't want to do that. I want to have the same space complexity I want to generate a size of K elements. So what I'm going to do is I'm going to have For I'm gonna have a min heap and then I'll check if the value is greater than the top value of the min heap Then I would pop the the top value of the min heap and add the value into the min heap, right? So that's basically the gist of this. So, okay, so once I add the occurrences of each Get all the occurrences for each each word I'm going to add it to my priority queue. So I'm going to create a priority queue of a min heap. So Okay, so this is actually going to be tougher okay because Okay, I'm going to call this min heap Because we have to add it we have to change our comparator. So I'll do that later, but for now We have to pass in a comparator because the heap doesn't know The the min heap because we're there's two values in it. It doesn't know which one Like which is prioritizing which right? It doesn't have like a compare comparator. It doesn't know that so I'm gonna have to we have to pass in a comparator to do that Okay, and also they want us to do lower alphabet comes first So that also changes things a lot because the min heap originally if you use a pay use a pair It would just compare the first value The first pair not actually the second at least how how I think it does I mean it might not do that boy, but That's how I think it does it But you could try it out if you you could try not passing a comparator, but it's actually good idea to pass in a comparator anyway, so Okay, so before I pass in a comparator. I have to Have Make it a pair because I'm gonna pair up Did the current word and then occurrences and I'm gonna add each pair of my occurrences in my map into my party queue So I have to do that so I will pass in Pass in pair and then I have a vector and that vector is gonna be the same of pair Okay, so now at this point We have to if I leave it like this It's gonna be ambiguous it's gonna be a old it'll be a max heap Right because of the originally would be a max heap. So I have to actually pass on a comparator. I'll write the comparator later But for now, let's assume that The comparator works, okay, so now I'm gonna go through all my for every Value inside my map. So let's say for every frequency. We'll call for every frequency inside occurrences inside my map I'll do it like that. Okay What am I gonna do? Okay, so Hmm, okay, so what am I gonna do? I'm going to Okay, I'm gonna get frequency dot first It should let me do that. Okay. Yeah, okay. It does that. Um, I'm gonna do min heap Let's see Okay for okay first one. Okay If the min heap Size is less than K because we're generating a min heap of size K If it's less than K, I'll just add The pair into the min heap right so I'm gonna do min heap Dot push and the pair is gonna be frequency dot first and frequency dot second Okay, so I think in my previous videos I actually like swap the pairs because the reason why I did that was to avoid writing a comparator But because now we actually have to write a comparator because they want the lower alphabet order to come first I'm not gonna change the ordering of this. Okay of how I'm adding the frequencies to the the each pair into the priority queue so I'll just add it like that. Okay So I'm not changing the ordering of the frequency. Okay, so otherwise What I'm gonna do is I'm gonna take the first top element. So the top element is gonna be the Let's see Yeah, okay, if the Frequency dot second frequencies. So frequency is each Each each pair inside the map of occurrences, right? So frequency dot second is the number of occurrences that we added for for each value for each string for each word for each word Has their currencies, right? So we kept track of that so frequency dot second is gonna be the the actual number of occurrences for each word, right? So if that number is Greater than the top value of our min heap So the the top value of our min heap is A pair, right and we paired it with the string and then the actual integer In the integer with the number of occurrences so I have to do min heap top and That will give us a pair and then I have to do dot second and that would be the actual second number Which is the number of occurrences, right? So if If the frequency dot second is greater than min heap top top second That means that we have to remove the value of our the top of our min heap. So I have to do min heap pop Then I'm gonna push the actual value onto it. So I'm gonna copy this and then do that Okay, so then at the end of this iteration assuming our comparator works and assuming that All this min heap is fine What am I gonna do? so After the end I need to get return the K most frequent elements. So at the end the min heap is gonna be have all the K most frequent elements and I basically have to iterate through my min heap and then add it to my array. So Vector string is a vector that we they require us to return vector string. So what I'm gonna do is I'm going to create a Vector string that has size of K This is gonna be to return. It's gonna have size. Okay. That's so this is like dynamically allocating that So I don't have to call pushback, right? If I call pushback, I'm resizing it. Well Let me see technically, I don't think it really matters that much, but Yeah, that's what this would it would be. Okay. So now your answers should be sorted by frequency how those if Okay Okay Then I'm gonna do while while my min heap is not empty. So while my min heap is not empty. I'm gonna remove the Top most value. So I'm gonna pop the top most value, right? But I need to get the for the top value and then push that on to my my to return, right? So to return add that to my to return. So my to return is because I just allocated a size K I'm gonna create a Okay, so I'm gonna create So Let's see think I got to think I think okay, so I'm gonna create a index for the starting an Index for the starting and then I'm going to do to return at index is gonna equal to the The top of the heap Okay, so the top of the heap is going to represent Remember is the top of the heap is the pair and that that should be the case the case frequent element If I record correctly, so that would be the case most frequent element But now because it's the case most frequent element You want the Let me see 32 I have to do it add it reverse direction. I think because Okay, so in this case The number of currencies being four three two one respectively if Wait, let's see Okay, let's just see if this works. I think that I don't have to add it reversely, but I Think I do. Okay, but I don't know Okay, so another rule cool thing you could do is that you could do index plus plus here So in that case that it would add in the index afterwards, so it would increase it at the index Of your value afterwards, so this would actually so I don't have to write in Index plus plus at the end of this so this would be to return It's gonna set the value of to return at zero It's gonna be the top value and then it'll set to return at one is gonna be a top value To return Tuesday go top value. Yeah, and each time it does that I'm gonna pop the top value. So the heap changes This part I have to do top value dot first. I think yeah first because the first value is the pair of The word the first value is actually the word and then I put the occurrences Accurrence as the second value. Okay, so after this I'm gonna return to return Okay, so now now now that that now that this is over with There's a there's an issue here We have to make this into a min heap and to do that. We have to change a comparator So what I'm gonna do is I'm gonna create a struct called a compare a comparator, yeah Comparator and then I'm gonna do this So this is how you would create a your own Comparator comparator. Okay, so I don't remember syntax off of the top of my head, but I Think this is Let's think it's passing comparator to pride. Let's see cat pass in pass in comparator into priority queue C++ Okay, so this is a good website that tells you how to do it. Oh You could use you can use a lambda function Okay, that's actually cool. Okay, I'm gonna actually use a lambda function because I don't want to do Maybe I shouldn't create a struct so originally I was gonna do this where you pass an astruct and then it would do Where you tap the operator, but I might actually do the lambda function away because that's much more efficient And it's a new version that people do nowadays. So I'm gonna do auto compare is gonna be a Left-right bracket Pear okay, so this is what I'm gonna do Is this in Okay, so it's not it's not a new function. It's just it's just inside here. Okay So I'm over here. I'm gonna do auto compare left bracket right bracket is Auto compare is equal to left bracket right bracket and then it's gonna pass in the two values that you're comparing so For in our case will be pair string int Let's not let's not do first. It's gonna. Let's do um, I don't want to do for a second Because that's already that's already confusing. Let's do I'm trying to think XY yeah, okay, let's do XY. Okay pair string int y Okay, so we're gonna have a pair of We're comparing pairs right of string and int here and then what they want us to do. Okay, so once you have the brackets Okay, we're gonna do a ampersand here and ampersand here just to make sure it's passed by reference We're gonna do a semicolon and then what we're gonna do is we got to return which one is greater okay, so um So based on this If the If I and love have the two most frequent words, so if the frequency of both words, okay This should this should be it wait. I should name this word one word two Yeah, instead of XY should do word one or one two. Yeah, one two. So I'm gonna do one two Okay, that's just represent one and two. So if so I'm gonna do I got to return a Boolean that represents which goes first and which goes second based on the comparison of this. So if Okay, so Okay, if one dot Second is equal to two dot second. I don't okay Okay, maybe I shouldn't do one dot second Okay, I'll do XY again. I don't want to confuse you guys. That's what I don't want to do Okay, so Just know that these are the two values. Okay, so if each pair Each pair that we're comparing if they have the same if they have the same exact Same number of occurrences, right? That's why I do dot second. That's the second part If they have the same number of occurrences, then we have to check The one with the lower alphabet comes first So to do that we have to use a compare to based on the Based on the first The string right so I have to do So I can't remember off the top of my head How men heaps if I want to create a man heat How it does it so I'm gonna look at this again So for their case when they did when they push three five one eight and they did their comparator Their priority queue. What is what is their priority queue look like? No, okay. What is their output? Okay, so here. Oh wait, this is greater There has to be they should have one of their priority cues If left, okay, if first lesson second is that the Let's see push one two three one eighty-five seven. I want to see something real quick Why don't they have they don't have the actual output that's crazy That's that's annoying. Okay, so for them. They did greater. I don't know what this means in C++ But if you use greater you make it a man heat, right? But I don't know what the what greater does. Let's see STD greater Okay, so greater returns X is greater than Y Okay, as their comparator. Okay, so if I want to create a Men heaped I have to do X is greater than Y. Okay so if X dot second is equal to Y dot second I have to the one with the lower alphabet order comes first so I have to do return So I want to create a men heap so in order to do that I have to do X dot compare to what? I think that's how it works X dot first dot compare Y dot first and after if that's greater than zero so that means that X is greater than Y the first string the look Let's see. They want lower alphabet comes first. I'm not sure what exactly is that I Think that's what this does This doesn't mean he right so Okay, so otherwise I have to return Let's say they don't have it. I want to return a men heap So I have to do return X dot second is greater than Y dot second X dot second is greater than Y dot second X dot second is greater than Y dot second. Okay Okay, and then what's cool about this is that you I could actually use a one-liner to make this Look cleaner, but I'll do that later when you guys understand it Okay, so now after this I have to pass my compare into this function into my priority queue and that's How do I do that here? Deck deck will tell you to compare. Okay. Yeah Wait, did I pass it into the priority queue also? Yeah, huh, that's weird, so they do that they do this also Okay, so I'll do this Compare and then I have to pass it here as well compare Okay, so this should ideally work. I'm not sure though. I'm not like a hundred percent sure But I think it does so let's submit like this is different from the solution of answer Okay, something wrong with a lambda function return first dot compared X dot first dot compare Y that first What I what expected is it? Oh forgot a semicolon my bad Okay, wrong answer the day. This is sunny day. Okay So in our to return right the top value is going to be the Kth most value right the Kth most frequent value we want the order to be the opposite way because they want the the order to be based on the highest to lowest and And So in this case is the is sunny day V has a value of four three two one So we actually have to start at the last part because We have to change the order it because our min heap is gonna have the first value first I think the Kth first I think I think that's what we do but Yeah, we're gonna have to start a K minus one and then we're gonna do Index minus minus until we reach down to zero So submit again. Okay wrong answer. I love okay, so I love means that something's wrong with our comparison so they want they want the They want the small lower one to go first lower alphabetical goes first so X.compare y is greater than zero that would be put the greater one first so in this case I should make it lower one. Yeah Okay, and I got accepted. Yeah, so that's basically how you would Finish this do this question using a min heap That that is literally how you would do this question if you do it with a min heap What we could do is we could actually make this a one liner using a ternary operator so I'm gonna do return make this one liner just to Make it more efficient. So if x dot second is equal to equal to y dot second I'm gonna use a ternary then I'm gonna return x dot first.compare y dot first is less than zero Otherwise I will return x dot Second is greater than y dot second Yeah, and then now I have to I'm gonna take out this so this is exactly the same thing code, but I'm gonna put it in one line It's like I don't think you have to put it all in like Two lines or something let's submit Yeah, except it. Let's look at other solutions. So okay sorting you could do that But I want to see that how they do it with the actual Heap okay, so they don't use they actually themselves didn't use a heap Okay. Yeah, he he accepted. Let's see So they did the same thing Except oh damn they actually added all the values to their so they created a max heap They actually added all the values and they popped the the came values from the beginning. That's what they did Great, and then the K they pop it. Okay Okay, yeah, so that's what they did here and then Yeah, they actually had to call reverse So we had to do something similar like that because we have to add the values for to the end and then to the beginning Because of the heap order, but yeah They did the same thing Had to push the values here. Did they use a max heap? I'm not sure actually Okay, so how do they compare they compare word one word two? If their currencies is equal to their currencies. Yeah, they compare word two is Greater than word one Did we do that? Yes, we did Word two is greater than word one. Yeah, we did that then otherwise we subtract x dot count dot Get w1 is greater than the currents of first one's greater than the second one. Yeah. Yeah, that's what they did. Okay Well, so they did use a min heap Yeah, okay, so yeah, that's basically how you do the same question. We Yeah, we basically did the same thing what they did. I want to see the fastest though. So before we Which one got the most speed? Oh, okay, besides less than K. Okay So they used They use a max heap They popped They removed K times from the beginning. Oh, okay. Yeah, so this guy use a max heap and remove K times in the beginning Is this faster? I'm not sure it might be faster, but it takes more space. It's a problem. Oh And oh, you don't even have to Add the values yourself you could just pass this in pass your frequency list. I didn't know that Let's change that. Okay. So instead of adding the values ourselves. We could actually just create the list like this But I don't actually we can't change that because then we have to our size won't be K It would be n All right, they created their size of n and then they popped K values from the top So our space complexity is better than that, but even though it's slower. I Want to see who actually got the best space complexity Okay, so his space is O of n. Yeah, that's the same same thing as a thing So our space is log of o of K log log of K. That's our space, but I don't know if you could get Same time same thing Okay All right, whatever. That's basically how you do this question rate com subscribe I hope I didn't spam you guys with the videos Lately, but I want to finish all the Want to finish all these K questions of heaps because they're really important. Yeah rate com subscribe. I'll show you guys later. Peace