 What's up guys, Mike the Coda here. Today we're gonna go over the coin change problem, okay? On hacker rank. Now I know I haven't done any algorithm problems in a long time and I haven't done that many data structures. I've been mostly doing data structures but I think I wanted to go back to algorithms. I think algorithms are kind of, I don't really know that much about DP so it's kind of nice if I teach you guys dynamic programming also and then I also go learn along the way also. So yeah, this is gonna be a problem called the coin change problem. So essentially as you're given a amount of sense, right? The total sense, yeah. So let's say we have total number of senses. So like when I say sense, let's say three cents. So here n is equal to three. So I have three cents. Now I'm given the C array which represents the different types of coins I could pick from this. So I have like eight cents and eight cents coin, a three cents coin, a one cents coin, a two cents coin. Now I want to determine how many different ways of change I can make given unlimited supply of each of these type of coins. Of eight cents, three cents, one cents, two cents. So for example, one way to make a change is essentially is that I can just repeatedly pick one. Right, I just repeat pick one. So let's say I pick this first coin of one. So then my total sense will now be two, right? My total sense will get decreased by one. So now it's two. And let's say I pick one again. Then now my sense that I have remaining is gonna decrease by one again. So now it's gonna be one. If I pick one again, my number of cents I have will be one minus one will be zero, right? So then my essentially is my bag that I'm picking from. My current bag will just have all ones, one, one, one. And that's one way you could pick the one way to pick these coins in order to get a value of total sense of three, right? Because one plus one plus one gets you three. So another way you could pick is by picking one and picking two, right? So you have pick one and then I pick two. So then my current sense would be three, right? And the final way you could pick is pick three. So I just pick three. And then my current sense would just be three. So as you can see here, the order does not matter, right? So if I pick one and then pick two, that's exactly the same as if I pick two and I pick one. So here they're asking for how many different, how many ways change can we make? So they're not counting about orders not matter, okay? Otherwise there would be way more ways you could pick. Another thing that you have to notice is that here you cannot pick eight, right? Because eight, this coin of eight has eight cents and eight is greater than my total sense of three. So if I pick eight, I'm gonna have negative sense in the end and I don't want that, right? I can't have negative senses, right? So like my total sense I will have is three and I cannot pick eight because of it. It's too large. It's greater than my total amount that I want, right? My total amount I want is three. So I can't have eight. Okay, so yeah. As you can see here, these are the different ways to pick this in another example to do it. And yeah, so I'm gonna explain how to do this problem and by using a table form. So here I'll just go pick, open this. So here, let's say I had a, open a spreadsheet. So what you could do this is you could use something called dynamic programming. This is a dynamic programming problem. And I'll just explain to you guys how to do this, okay? So what we're gonna do is essentially is that we are going to have a row and we're gonna have four rows and three columns, okay? And the reason why we have four rows is that we have four different types of coins we could pick from, right? So here we have eight, three, one, two. So in this case, we could pick eight, three, one, two. These four rows will represent like whether I'm picking this current coin or not. So if I, in this case, eight, three, one, two, right? This is gonna be your row. I'm actually gonna actually unhighlight this because it's kind of confusing. Yeah, so this is the type of, this is gonna be our table, okay? Four rows, four rows, and then three columns. This should actually be, yeah, here, here I'm gonna unhighlight this also because it's also kind of confusing for you guys. Okay, so this is like the table that we're gonna build and each of these will represent whether I'm picking the current coin or not. So here we have four rows which represents the current coin that I'm picking up. And these three columns will represent like the total sense that I have, right? Each of the senses. So I have one, two, and three. So N represents like the total number sense I have, right? See, this is gonna represent like the senses if I choose at this current sense, whether it's less than or greater than the coin that I'm picking up, right? And then what we're gonna do is we're gonna build this table. And each of the row represents if I pick this row, pick this current coin, whether or not, and then while we're building this table in the end, this last value is gonna represent our end result of the different choices that we're making, okay? So in this case, what I have in this current code is I build this table of the size plus one and then this of C, size plus one. So I actually build this five rows and I actually have four columns. The reason why I do this is because I'm actually, I wanna index from index at one because yeah, sometimes you have issues if you don't index from one. I'm indexing from one because the coins I pick later on be later on. But essentially it's about the same thing as doing indexing from four. But yeah, so what I'm doing is I have like an extra row in case if I go out of bounds. But yeah, this is the extra row. Yeah. Okay, so the first part of the code, what I'm doing is I'm assuming that I pick each of these choices, I'm gonna assume that I pick all of them, okay? So the first choice I'm doing is I'm assuming I pick this coin eight. I'm gonna assume I pick this coin three. I assume I can pick this coin one and I assume I could pick this coin two. So one represents I'm picking this coin and whether I'm picking this coin or not, okay? So that's what this is. So in the beginning I'm just assuming I pick, I could pick all the coins, okay? So in each of these states, each of these rows represents assuming I pick the starting value of eight. So if I start this value of eight, that means I assume I pick this coin eight and then I wanna see what can I build off of picking this coin of eight. Here, this three represents if I pick this coin three and I'm assuming what I could build off of if I pick this coin three. And one represents if I start picking this coin one and I see what I could build off of picking this coin one and two represents if I pick this coin two and see if I build this or not. Okay, so what I do here is I loop from zero to the size of my rows. So here zero to the size of here. So this will represent all the columns, all the different types of coins I could pick from. So yeah, I'm gonna loop through from zero to each of the coins I'm picking from. And here I'm gonna start, I'm gonna loop from one to the total number of cents I could have. So the first essentially is I wanna check from one to three. So I wanna have a total of three cents, right? So what I'm doing is I'm gonna loop through from all the values from one up to three of a total of three cents and see if using each of these values see if I could pick the coins that I have out of my coins of bags that I have, see if I could pick those. Because if I could pick them, then I'll just add one to it. And if I can't, well, then I can't pick it, right? It's essentially what it's doing. So yeah, so here I'm looping from each of the coins. Here I'm gonna loop through from one to the total number cents I have and see if I could pick these values. So if I is greater than zero, that means that I'm picking like other values. So I represents like the rows and here J represents the columns, right? J represents the columns here. So I represents each of these rows. So if I'm not at the first row, that means I'm going to add by the previous rows, right? Because my current state depends on my previous state that I'm currently picking from, right? So essentially it's also me who means that we're excluding if we don't wanna pick this row. Okay, if we don't wanna pick this current coin. But anyway, my current choice does depend on my previous choice. So that's why I do this dp of i, j plus equal to i minus one. So i minus one represents the previous choice that I made. And I'm gonna add the value of the previous choice. Reason why I do this is because I'm assuming that all the values I'm picking each of these coins first. If I'm not at the first coin, I'm gonna assume that I picked the previous coin already. So I have to add one regardless. I have to add from the previous value regardless. So that's why I have this. Now here it gets a little tricky. So here what I'm doing is since I'm looping from j from one to the total sense, I'm gonna check each of these values if I could pick each of these coins, okay? So here in this case, I'm checking, hey, if I'm looping from one to all of my total sense, can I pick this value eight, right? Can I pick this value eight? And if I do, then I need to add from my previous values of picking this eight. So if I can pick it, then I need to add my i and my current value and the j minus the previous value. And the reason why I do j minus c of i, so c of i represents the cost of this coin, right? c of i represents this eight, this three, this one, this two, right? So if my current sense then looping from one to the total senses, right? Subtracted from each of this coin is greater than or equal to zero. That means that I could pick this coin. So like if, let's say my coin was like, my total sense was three, right? Based on everything. Well, if I could pick this value of three, right? Then I'm gonna do three minus three, and then my total sense in the end would be zero. Because it's zero, that means I could pick it, right? I could pick this value, right? If it's greater than or equal to zero, that means I could pick this value. Whereas if I try to pick eight, three minus eight would give me negative five, right? And I can't have negative five cents, right? This n represents the total number of cents I could pick. So I can't have pick a value that's greater than it. Otherwise I would get a negative value, and I can't have negative sense. So that's why this is checking. If my sense then looping from one to the total sense, if I subtract by the current sense of the current type of coin that I'm picking from, and if it's greater than equal to zero, then I'm going to include it, right? I'm gonna do DP plus equals DP of I, J minus C of I. Okay, that's what this is saying. That's what it does, okay? And this, the first if statement is assuming if I exclude it, right? Like I'm not including it. But because I set all the DP values to have one in the beginning, I'm assuming I include all the values. So in the case where this if statement never runs, then it's just gonna add the previous value of it and exclude the current J minus C of I, okay? So yeah, that's what this if statement does. So assuming if, it's assuming like if we exclude it, hey, add the value anyway, but if we're not excluding it, then we're just going to add DP of I and J minus C of I. So yeah, that's essentially this, both of these building this DP array. And then in the end, we're just going to return C dot size minus one in N, okay? We're gonna return the Nth value of our current DP array. So yeah, that's essentially the gist of this problem. This is like how I've coded the solution. There's other ways to do it. I know some people coded this instead of assuming that you could pick all the values of ones of each of these coins. Yeah, I know some people start at zero. And then what they do is when they include something, they're gonna add by, when they want to include this, they'll just add by one for each of the coins. If they don't want to include, they add by zero. They just have this equal to it. But in my case, I said all these, I'm assuming that I pick each of the coin first. So that's why I have equals to one. So that's why I do it this way. But if you didn't want to do it this way, and you want to start at all the coins starting at zero and having one to include it in zero, if you're not included, then you would have this to all equal to zero. And then this part you would, if you're excluding it, you have to check if it's like, if you're including it, you would do plus equal to this and you would have an else statement and then it would equal to the previous value, right? It wouldn't just be like, it wouldn't be plus equals. But yeah. Okay, so that's basically the gist of this problem. I hope you guys enjoyed this video. It's essentially just breaking this problem down to each of the sub-problems and adding the values back up. And then once you get that, that's the end of it. Great, I'll subscribe, I'll check you guys later. Peace.