 What's up YouTube Mike Dakota here today? We're gonna go over the next problem of the previous contest that just ended about 30 minutes ago, but yeah It's a two two four four minimum rounds to complete all tasks This problem actually took me an hour to figure out actually more than that actually I think I solved this like at the last nine minutes of the contest, but which is surprising though. Yeah Okay, so you're given zero indexed array called tasks which represents the difficulty of a task and In each round you could complete either two or three of the tasks of the same difficulty level Return the minimum number of rounds required to complete all the tasks or negative one if it's not possible Okay, so let's actually just like Go over explain like the first test case because I know like This might be hard to understand So if we if we go through here, okay, so let's just copy this So these are our tasks right two two two three three four four four four okay, so they saying that we can complete Either two or three tasks of the same difficulty level So that what that means is like So we have two two two three three four four four right? So if I want to complete the task difficulty of level two Right, I could either cross out two of these Right, right, and I'll be like one round Because I cross the two of them or I could cross out Three of them right so across that Three of them right three of the twos and I'll be one round. Okay? So you guys understand what I'm doing, right? And then um, yeah, so that'll be one round So we plus one for that one round and then For three I could cross out Either three of the three the threes right or two of the threes So because there's no three like there's no three threes I only could cross out two of them right because there's there's only two of them here So I'll just cross out two of these threes so then that adds another one for the number of rounds So then I'm at four four four four four four four right these last part So yeah, I Could cross out three of the fours first right so I could do one two three so then that adds another round and Then then I could cross out the last two fours so cross out these and adds another round So in the end we get like one plus one plus one plus one four. Okay, so this would be the minimum amount of Rounds necessary to complete all the tasks right because four would be the minimum that number rounds to complete all the tasks Because yeah, we complete all the tasks and that's the smallest number rounds it would take now the reason why this is the smallest number rounds to take is because It just we it just took The smallest number rounds to take um if you if we had done something else like instead of crossing out three But we cross out two every single time so Actually can't even do that in this case Well, let's say there was no this for right. We can only could just cross out Two again and then two again, and they'll be like and then another two To assume let's say there was another four here. Then we cross it to again. They'll be like plus three and then With all the rest of the three here, they'll be like six, right? remember before we had like Think three other rounds prior here So yeah, that's why it's that's not a good idea. Okay, so how do you do this problem? Okay, so Actually, let's explain task number two first because Yeah, the second not task number two the The second test case first before we explain how to do this problem. So we have two three three three This is gonna return negative one because it's not possible and the reason why is because let's say across out three Right, so I cross two of the threes out So then that's one round, but then I'm left with just one two and Remember we only could cross out three of the same kind or two of the same kind Right, so here we can't cross out three of the same kind or two of the same kind because two is just by itself So it's impossible. We can't cross the two out. So the answer is just minus one. Okay So that's just that so How do you do this problem? The simple matter of fact is that we can use a greedy algorithm and Basically greedy algorithms are just explaining like the best chance you could do at the local time So as you can see here, we have like numbers two three and then four, right? Once you see this problem, you could actually think about what we need is like an actual frequency array because Remember we're crossing out Two or three tasks of the same difficulty level. So instead of actually like just crossing these part Each of these out and then like re like reshape the whole array Easiest ways actually create like a frequency table of each of the numbers and then then we could just like subtract How many we're getting rid of instead of actually, you know, we're moving them from the array So if we build a frequency table, so let's say how let's say Number two how many how many number twos are there here? There's three of them. So we're just gonna put three right How many threes are there here? There's two threes right one two. So we're just gonna put two here and how many fours are there? There's one two three four five. So we're gonna put this five Okay So these are the different frequencies right I Guess I should say like number and then frequency, but I hope you guys understand so the right Five is like the one on the right sides are all the frequencies of each of these numbers okay so now Remember in each round we could either complete two or three tasks of the same difficulty level. Okay, so because we want the minimum number of rounds It's actually better if we take three tasks first all the time Right because if we pick two of them Like if we just remove two every single time, we would have more rounds to take to remove, right? Like if I'm at So let's say I want to remove all the values of five, right? So Let's say I should remove like try to remove twos from this five so I removed two of them so like So here I'm gonna remove two of these so remove four so minus two is gonna be three of them are left Right, so then this would count as one round, right because I that was one round of subtract two Then I have to subtract another two here And this one minus two And give me one so that's another round and then if I had more here I would subtract another two here, but let's say for the sake of it That would be another round right Whatever left over we can't really do this but like let's just say for the sake of it. We could subtract the last one, right? This would take three rounds right This whole thing would take three rounds whereas if I just instead of subtracting Jews every time I subtract three Right, so if I take five and I remove three move three of these four is first I Would have two left and They'll be just one round and if I remove another three first Let's say for the sake of it. We can okay, and I'll be another round and Then here we would get we would have a total of two rounds Okay, so it's much if you want a minute minimum Minimize the number of rounds we take we should remove as much as possible Right. Do you guys understand? Like if we want to minimize the amount of rounds it takes to To get rid of all these values We want to remove as much as many values as possible from the array first, right? So that's why it's most optimal to remove three Three tasks of the same kind first and then remove two when we can't remove three Okay, so that's basically the gist of the greedy algorithm Okay, so we're just gonna keep removing three tasks of the same kind first and then remove two. Okay So what in what situation can we not remove three though? and that We can't remove three if we don't have three right so in let's say we have in this case The number three here these two threes. I can't remove three here because I'll get negative Right, like they don't want us to like we like we can't remove three if we don't have three right They said it must be Difficulty a task you you have to remove either two or three tasks. So if we don't have enough Threes and we can't remove it. Okay So that's that's that's what we have to do. Um, so yeah So then what other case can we not remove threes? Let's say it was like Like even so let's say we had So in this case of four right, let's say this four was not here So let's just let's just ignore one of the fours. So let's let's ignore this last four, right? Let's let's let's assume this was like for Regular value four. So in this case if I remove three here I'm gonna have a lot of value of one and I can't remove three again right, so I'm not gonna be able to remove all the values all the fours here Right, I can't just remove three and then minus three again So in this case what I only could do is just remove two first because I'll be one round and Then I'll get two left over and then I have to remove another two So then I get zero and that'll be another round. So then I would have a total of two rounds right, so in Situations where I can't remove three There are certain situations where you cannot remove three. So yeah In this case, you only can remove two. Okay So yeah, so how do you do this problem? The most basic way is after we build our frequency table, I'll show you guys the code Yeah, so after you build our frequency table We first have to check if Any of the values of our frequency table is less than two and the reason why is because in our second case right here two three three if any of the For two three three since the frequency of two is just one. We can't remove We can't remove this two out because we don't have enough, right? So the answer would just be minus one here, right? because It's not enough to is the remove we only could remove three of the kind same kind or two of the same kind So in this case two is just by itself of one So we can't remove three of them or two of them because we just don't have enough to remove So in our case if any of our frequency that we have is less than two Like in this case, we have to return negative one. So yeah here. I use an unordered map Dictating the frequency of all the values that we have here So two three four I build a frequency table of this you could use you could use a In Python you can use dictionary in Javascript, I don't know what it is You could try to use an array, but an array would you have to build like a gigantic super large array? If you want to do that that way Yeah, so here I build my frequency by going through every single tasks and for each value at the certain task I add by one and that builds up my frequency array frequency table Now I'm going to loop through my frequency table and if any of the values are less than two I return negative one and the reason why like I said before is that they have to have They have to we have to have enough that Enough for the same kind to remove it, right? So if it's just one or two If it's just one or zero then we can't really remove it, right? So that's why if my Frequency of whatever number is less than two. I just return negative one Okay, this is the part where it starts getting tricky to understand but yeah, okay, um I'll just explain it So let's say we have I'll explain the first case. So let's say we have let's say we look at two, okay, and If the number two since it's three If if our number is like divisible by three It's most optimal to just keep subtracting by three every single time, right? Like I like how I explained earlier in the video. So Yeah, if it's If it's divisible by three Then we should actually subtract by three every single time. So let's say this Let's say the frequency of two is not Not three but like nine ish or something. Let's say this was nine Okay, let's say the frequency of two was nine in our case. It's better to just keep subtracting by three over and over again Because then we'll in the end we'll get to zero right because it's divisible by three So while it's divisible by three We could just keep subtracting by three every single time. So what is the difference? If I take a number and keep subtracting by the same number over and over again What is the equivalent of that? Simple it's the same thing as dividing, right? So if I take nine I keep subtracting by three over and over again How many times am I subtracting by three? Well, I'm subtracting by three three times Right. So for each of these frequencies, we could just like if we want to count how many times we're subtracting by three We just have to divide it by it. So Let's say they were the freak Let's say the frequency of nine was nine all we have to do is nine divided by three We give us three and this would be the count of how many threes there are That we subtract from every single time And this would be like the number of rounds that we take right so yeah So that's what I do in the first part of the for loop so if If my the free if the so I go through every single value in my frequency If it's divisible by three Right if it's divisible by by three What I'm going to do is I'm just going to um add The frequency of it divided by three. Okay. So rounds plus equal to frequency of it divided by three So like In this case, let's say we have two or three. I'll just divide by three And give me one And that'll be the number of rounds it took right. It was just one round To get rid of all the tubes here Okay, so that'll be plus equal one to the rounds So, yeah, so while it's greater than zero. I just divided by three Um, and I add number rounds and then here I just get the remainder of it because I want this while up to end So if I take the remainder of three divided by three, it'll give me zero So then I just basically reset my frequency Of this three is going to become zero right because here if I Got rid of three of these twos It's going to become zero Like in the end after we remove all the twos will become zero Of three of these becomes zero. So, yeah, I just mod mod equal by three And then this would this four this while loop would become zero Uh, yeah, the frequency would become zero and the while loop ends here So yeah, this is that that's the first case the first case is if it's divisible by three And we could just keep subtracting by three over and over again. Okay All right Here's the part where it gets a little tricky. All right. So the part there it gets tricky is if we're In certain cases where this starts getting tricky so Let's say I'm at let's say I'm trying to remove values of four. Okay Well, if I just subtract by three Right, um for four If I just subtract by three That'll be one round. Right. So I got rid of three of these fours And then I have two left, right? In here, I can't subtract three again, but I could subtract two So I could just subtract two And I end up with zero And then I got rid of these and I got plus one. There'll be another round And then again, I would have two and the number four becomes zero Okay, so that's This is an easier easier way, right? This is like an easier example, but what if we have a different example? So Let's say we had I don't know eight fours This is the part where it gets really tricky. So let's say one two three four five six seven eight One two three four five six seven eight. Okay So in this case We have eight fours So number four is here is eight the frequency four is eight So if I take eight and I just keep subtracting by Three as many times as possible So it's essentially just dividing by three Right. So if I take eight and divide by three What is eight divided by three two six two Okay, so it'll give me two so we're going to remove Two of these eights. So it'll be one two three Right, and then we subtract one two three and I'll be another um Four that we removed right so we had four of these were moved So that'll be one round and then another four of these were removed. So that's another round So we had We had two rounds. We took two rounds to remove those Then I have something left over I have two left over and I can't subtract by three anymore. So I have to subtract by two Right. So here I would just subtract by two and this would be gone So then two minus two be zero And then I would have to have another round plus one here. So then I would have three rounds. Okay So that that's the just this part, but so that's a harder example, right? So you might be thinking, okay, so if if I want to Get rid of like threes If I want to get rid of all these values Simple ways to divide by three and then try to divide by two afterwards because I'll get me the answer to this problem Right, divide by three divide by two. Um, it's much harder than that actually Because let's say we had Let's say instead of fours Instead of eight fours. Let's say we had 12 fours Or maybe maybe not 12. Um, what's that example I used? Uh 11 fours. No, no, uh seven fours Seven fours. Okay. So let's say I had seven fours Two three four five six seven Okay, so we have seven fours here In this situation it is much harder because So five seven fours All right, take seven divide by three I'm gonna get um two Right, so then I remove Uh, three of these twice. So here this would be one round Right, and I remove another three of these twice So another round So I get me two, but I have a four left over Right, so I couldn't I didn't remove all the fours here. I have one one left over Right, so you might be wondering. Okay, if I have one left over I can't actually remove these values So that's the thing is that instead of We can't actually remove Three twice in this case It's much more optimal our optimal case would actually be to remove three ones So we have seven, right? Um, I'm gonna remove three ones. So I do This right so then our seven minus three we come four And then from four I could just remove Four twice, right? So I just have to remove Four twice so I could do um Four minus two would give me two So so here at first we have removed three Three fours, right? So that'll be one round Then four minus two I'll remove two of these There'll be another round So then I have two left over then I could do two minus two would be zero and then All these got the values that would be gone and all my fours would be gone, right? So In our optimal value in this case We couldn't remove two threes, right? Because then we had one left over It's much more optimal in our case to actually remove One three And then remove twos at the end all the values of twos at the end So, um after doing like a bunch of cases, here's what I found that works best So Let's try to so first what we should do is we should actually see If we could actually remove it so So let's say I take seven Right, so I have seven what I'm going to do is I'm going to divide by three And this is going to give me two Right because uh seven divided by three two six one, okay So in this case My remainder is one in this case, right? so What I could do is that I could check if The remainder that I have is not equal to two Then I just subtract one from how many threes I could get Okay So in this case, um seven divided by three is equal Two two and there's a remainder of one, right? So there's two remainder one So because our remainder here is not equal to two right because I could subtract three every time and then I could subtract two every time, right? So if I have What I want is to have my remainder to be two Right because in the end I could just keep subtracting twos every time And then and then I could then my last value would be zero So all I could do is to say hey if my remainder is not equal to two I instead of taking Two of these right removing three twice. I'm just going to remove three ones. So Yeah, so Here I remove three twice, right? Because seven divided by three is two This time I'm going to say seven divided by three. Um, I'm just going to remove it To be two minus one would be one Okay, so then I'll be seven minus three would just be one All right times one would be one and this would give me four And then once I have four I could just divided by two every single time. So that's what I do here So here val is going to equal to Seven divided by three Which is the frequency we have of seven divided by three, which would give me the number of times I could do it if If my current value minus val times three is not equal to two So this is just checking if the remainder of it is So I'd basically check if seven times Seven minus three times Two here. So here I have seven minus three times two All right, so seven divided by three is equal to two Then I just take this number two and I put it back into here Into this number here and I do seven minus three times two Which give me a remainder one Then I just check if this is not equal to two Then I just subtract one from my value. So this This two would So this one is not equal to two. So I just subtract one from this two So just with one and then in my answer would be four here And yet right So, yeah, so then I do rounds plus equal to value So this means rounds plus equal to the number of times I divided by three. So It'll be one right because I divided by three once instead of doing twice So, yeah rounds would be plus one Then I just set a equal to the remainder Right frequency table to equal to the remainder I get from subtracting um val times three Okay, and then after I set the remainder here. So this remainder would become like four Because I subtracted three once right so my remainder would be four Then all I all I do is I increment rounds Of four divided by two So four divided by two would give me two And that increments rounds would be two so then my number of rounds would be um So it would be one for removing three once and then Two for removing two twice for seven And then yeah that rounds would be plus equal to two and then here I just like subtract um What I have left so then this four would become zero in the end Right four minus four would be like zero So yeah, uh, yeah, that's why I do here and then I yeah, so then it comes back to the top And then it continues for the other numbers of the frequency table And then in the end I just return number of rounds So, yeah, I hope you guys understood this gist of this problem This was kind of hard to explain But essentially what we're doing is we're just getting the remainder seeing can we If we can't get to As our remaining value that's divisible by two then we should uh, we should just decrease the number of threes we Subtract Okay, and then we could just keep dividing my two every single time in the end So yeah, that's basically the gist of the algorithm that I would code it up It might be an easier way to do this. I don't know Yeah, I hope you guys enjoyed this video very come subscribe. I'll check you guys later. Peace