 What's up guys my name is Michael and welcome to my YouTube channel today we are going to go over this problem by Lentini and gold coins basically You are given a coin and and it could be exchanged in a bank for three coins and over two and over three and over four But these numbers are all rounded down to make a profit You can also sell by Lentini and coins for american dollars and the exchange rate is one to one But you cannot buy a by Lentini and coin. You have one gold coin. What is the maximum number of American dollars you can get for it? So Yeah, that's pretty much the problem here. You have a one By Lentini and gold coin. It has a number on it The coin n can be exchanged into three coins and over two and over three and over four And these coins are all rounded down to make a profit You can also sell the by Lentini and coins for american dollars and the exchange rate is one to one Okay So how do you do this problem? um Let's just explain it now. All right guys, so Now let's think about what exactly do we have to do in order to solve this problem so Back to the problem statement. Remember we say that the exchange rate is one to one And that a n coin can be exchanged into three coins and over two and over three and over four So let's just try some numbers and see Well, if we could find a pattern. So let's say I have a n is equal to eight, right? So I could exchange just for n over two plus n over three Plus n over four Right, and this is going to give me eight over two plus eight over three plus eight over four And this is going to give me four plus two Which gives me four plus okay So this is just going to give me the exact same number As my original one and remember we want the maximum value, right? So you would just stick to the original Now let's look at n over seven Let's look at n equals seven All right, n equals seven is would be let's say n over two plus n over three plus n over four So this is going to give us seven over two plus seven over three plus seven over four And this is going to give us, uh, three plus seven over three three two six two plus Seven one one. This is going to give us six right so at this point you would only choose n equals seven right you don't split it you just stick with n equals seven right now let's try something else let's try 22 all right because 22 is something large n equals 22 so then n over 2 plus n over 3 plus n over 4 this is gonna equal to let's try splitting it I would get 22 over 2 plus 22 over 3 plus 22 over 4 this is gonna equal to 11 plus 7 plus 2 5 6 5 5 yeah and this is gonna get me 18 plus 5 which is gonna get me 23 so at this point I would choose this splitting I would choose the splitting instead of choosing the original value because we get more right this is more 23 is more than 22 so now our question is when do we split and when do we keep the same value so remember we go back to n over 2 and over 3 and n over 4 well apparently basically it turns out that you would actually split if you you would split more when you have greater than 12 so if n is greater than 12 you would actually want to split split so you would actually add these up and the reason why is because if you look at the LCM of 2 3 and 4 the LCM of 2 3 and 4 is 12 right the LCM of 2 3 4 at least common multiple is 12 and that's why when you get greater than 12 you want to split it because at the point when you get greater than 12 the value that after splitting and adding them up is going to be greater regardless of the original value of n no matter what you put after n is greater than 12 you are going to get a value that is going to be greater than the original value of 12 so anything less than 12 so let's say n is less than 12 right anything less than 12 you're just gonna keep keep the original value and just return it so now the big question is is that how do I make this faster though because if I'm just gonna keep splitting by 2 3 and 4 and just keep repeatedly splitting splitting for those values I'm going to get a situation where I'm gonna recalculate the same question over and over again right I'm gonna get the same value so like let's say I had like 6 over 2 and then in another splitting the 6 over 2 is equal to 3 right I don't want to calculate this again 6 over 2 equal to 3 over and over again so we're gonna do something called memorization is when every time we calculate a certain value for n over 2 or n over 3 and 4 we are going to store that specific value into a map and then every time we have the same value that's divisible by it again we are just going to look up the problem to it okay so that's what we're gonna do okay so yeah so back to the problem when I say that we're gonna use memorization in that the given n basically what I mean is like let's say we have like back to like 22 right 22 is gonna give us a value for for this part it's gonna give us the best possible value for the max is going to give us 23 right so then every time we if we're gonna ever look up the best possible value for coin 22 we're gonna store the value of 22 best possible value for 22 as a value of 23 right so then in the case of the next iteration let's say we had like I don't know 64 100 or whatever different value of coin and you went and when you split the values if we had to look up the best possible value of 22 again we don't have to calculate this again this state again we just have to look up and see if it once we have it we just added to we just return 23 so then we don't have to keep recalculating this part over and over again right and comparing if the left side is great or the right side is great right so then every time we compute something we're gonna store it into a hash map and then store the best possible value for that value in the hash map and then we look it up and then we just return it so yeah I'm gonna explain the code now and then I hope you guys understand what's going on our guys I'm going to explain the code now to you guys so they said that to keep reading in and a test case until there is the end of the file so because of that I had to do this long long N in a while CNN right that's gonna keep reading it until the end of the file and then I print out my method exchange which basically returns with the the answer to whatever test case it was okay whatever number and it was okay so now let's go to the exchange method so above exchange I have a map called s and this map just stores the best possible value for each test case right so if it was 22 it would restore 23 it was 8 it would store 8 right stuff like that okay so in my exchange function now if my current value and is less than 12 I just return n and I already explained the reason why we do this is because if it's less than 12 we know that no possible number can be greater than this right because if you do n over 2 plus n over 3 plus n over 4 it's always gonna be less than the original number so that's why we just return n and yeah okay now we have this this condition if s dot find n is not equal s dot end this is just checking if in our current map that we did did we already calculate the value of n did I already calculate the pot best possible value of n in the case to return so if it's not equal to the end this means that I already did calculate it so I just have to return it I don't actually have to recalculate it again okay that's what this this value means okay so if I already have a value inside this function inside my map that already stores the best possible value I don't have to recalculate it so I'm just going to return it now otherwise we actually do have to calculate it so I have a variable called to return in here I'm just passing exchange n over 2 so I'm gonna recursively call this original function and pass an n over 2 and that's going to keep calling the function again and again all right and then I'm gonna add it to exchange n over 3 and then add it exchange n over 4 so this is going to recursively call it and get the best possible value for n over 2 and then add it with n over best possible value of n over 3 and add it for the best possible value of n over 4 okay then after that I set my because we did not have the this value calculated yet right now the two returns are going to store the best possible value of n over 2 plus n over 3 plus n over 4 then I just store that in my map of s so s at the s at the n right for the nth value for whatever my n value is right I'm going to set that equal to the best possible value that just calculated and I just return to return okay so that's just going to return the best possible value that you just calculated so yeah that's basically how you do this problem it's just a bunch of recursive calls and then you just store each recursive calls value inside a map and then you don't have to look it up again all right so that's pretty much how you do it rate com subscribe I'll check you guys later peace