Coin Changing Minimum Number of Coins Dynamic programming

  Рет қаралды 505,386

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

Күн бұрын

Пікірлер: 261
@sharanbalajimanickamoorthy3639
@sharanbalajimanickamoorthy3639 2 ай бұрын
The best video on this topic and this example. I understand DP much better after this video. Thank you so much!
@ChaitanyaPaiTechie
@ChaitanyaPaiTechie 9 жыл бұрын
Dynamic programming is now getting easy for me after watching your videos. Thank u for posting.
@MatticusF1nch
@MatticusF1nch 9 жыл бұрын
All of your videos are nice and concise. Helping me a lot studying for an algorithms final. Keep 'em up!
@ericvantassell6809
@ericvantassell6809 Жыл бұрын
Amen to that. Many other youtubers take 20+ minutes on a less effective presentation
@shelllu6888
@shelllu6888 3 жыл бұрын
I have watched 4 different types of Money change Dynamic Programming videos, this is so far the best, beating the lecture video from Coursera Algorithm course. If you wanna learn it, watch this first.
@atabhatti
@atabhatti 9 жыл бұрын
Thanks for making this video. I do have a couple of suggestions for you that will help your audience substantially: 1. Explain why you chose to use dynamic programming vs. recursion vs. some other scheme. 2. Explain why you are doing the min and why each flow of information (either from above or from the left) exists. You explain the mechanics but not the structure of the solution so your video helps me understand how this technique works but does not give me any intuition into where else I might use this solution. 3. Contrast this problem from other similar problems. For example the problem to list the number of ways you can make change from a given denomination of coins for a certain amount. Otherwise, thanks a lot.
@22PARTHGUPTA18
@22PARTHGUPTA18 9 жыл бұрын
+Tushar Roy If you can add separate videos explaining why, they will be of great help and will enable the concepts to get the logic stick. Else these things are very difficult to remember later.
@YabeDayoo
@YabeDayoo 6 жыл бұрын
chicor2000 smart
@YabeDayoo
@YabeDayoo 6 жыл бұрын
chicor2000 3 years ago idk
@YabeDayoo
@YabeDayoo 6 жыл бұрын
IDC
@VikasGupta2014
@VikasGupta2014 8 жыл бұрын
In DP problems, please start with recursion so that we could understand why it was need of DP here. Anyway awsome tutorial. Thanks a lot.
@scabbage
@scabbage 7 жыл бұрын
agree
@jasonng3194
@jasonng3194 6 жыл бұрын
www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/ Here, they have explained why DP is better them recursion. Simply because you can avoid repeated sub problems. But don't look at their DP solution coz it is not for minimum coin change. But the concept is essentially the same.
@naveenchowdary7959
@naveenchowdary7959 5 жыл бұрын
he do not kow
@jk97322
@jk97322 4 жыл бұрын
@Arun Gowda In place of Matrix operation just call the function with same argument can give you a recursion solutions . You can refer geeks for geeks website
@shashanksagarjha2807
@shashanksagarjha2807 4 жыл бұрын
please check this playlist : kzbin.info/aero/PLeF0b8iqbx4mogykbd82-HY9Y1-JS9MDr
@lanar3528
@lanar3528 5 жыл бұрын
You need to explain how you go to the optimal substructure for this problem, that's what I'm struggling with
@shubhamgoyal2622
@shubhamgoyal2622 9 жыл бұрын
Until I watched these videos pertaining to the concept of dynamic programming, everything literally went overhead. Now I would safely say I'm getting in the realm of dynamic programming. Thank you.
@rahilthakkar6007
@rahilthakkar6007 4 жыл бұрын
thxx man it will save me an hour in examination...its jst amazing! truly grateful to you..
@CoolDivya1000
@CoolDivya1000 9 жыл бұрын
Thanks a lot. It looks quite difficult before your explanation, but your logic is very simple . And please upload a video for Assembly line Scheduling using Dynamic programming. Thanks again.
@qwarlock4126
@qwarlock4126 2 жыл бұрын
You are brilliant.. simply elegantly brilliant
@AsadShahabuddin
@AsadShahabuddin 7 жыл бұрын
Really great explanation. Thanks a ton.
@keshavrastogi685
@keshavrastogi685 9 жыл бұрын
We have to add one more condition in which we have to check that the row should be greater than 1 , then only we have to check the minimum of both . If we are in the first row , then we have to directly take dp[i][j-a[i]]+1. for(int i=1;i
@mister_bake2743
@mister_bake2743 9 жыл бұрын
+keshav rastogi Thanks mate, your code is helped me!!!
@stith_pragya
@stith_pragya 3 жыл бұрын
superb explanation and Thank You Tushar Sir....
@swadheenta.957VBTS
@swadheenta.957VBTS 3 ай бұрын
Nice explanation solved my doubt thank you 💜
@rishabh1993able
@rishabh1993able 9 жыл бұрын
awesome......I struggled a lot in this problem now it is clear.......thanks
@subhashreesahu692
@subhashreesahu692 9 ай бұрын
so quick and understanding easy to grasp
@11m0
@11m0 8 жыл бұрын
Oh, so this is kindda like the knapsack prob u did...really helpful to know...thx
@pre_ty_things
@pre_ty_things 3 жыл бұрын
I am not sure if we really need 2D Dp. I did in one memo[0] = 0; for (int i = 1; i = coins[j]) { memo[i] = Math.min(memo[i], 1 + memo[i-coins[j]]); } } } return memo[amount];
@ShivangiSingh-wc3gk
@ShivangiSingh-wc3gk 7 жыл бұрын
I like this one more than the new one, you speak so fast in the newer version
@abhishekgupta4360
@abhishekgupta4360 9 жыл бұрын
All of ur lectures are just awesome.. really great explanation to each of the dp problem.....
@cristianouzumaki2455
@cristianouzumaki2455 3 жыл бұрын
More appropriate title would say " infinite denominations of same coin available"
@joyshah_22
@joyshah_22 Жыл бұрын
Excellent explaination💯💯💯
@sharonbinto
@sharonbinto 3 жыл бұрын
Thank you so much tushar. Your explanation is really the best and when I tried to code with your explanation. It was very easy and finally when the output came. Really it builds my confidence that I can also code. Thanks for the video
@siddharthshah711
@siddharthshah711 8 жыл бұрын
Such a lucid solution! Thanks a lot! :)
@anymatix491
@anymatix491 7 жыл бұрын
Thanks sir for the solution ,been finding a clearly understandable video for this..
@my3m
@my3m 4 жыл бұрын
So much better explained than AlgoExpert
@qandrea1015
@qandrea1015 5 жыл бұрын
If 1 it is not in the coins. Just initialize T properly: in the first row of T we put +inf if j < "the smallest coin" (coin[1] in my case, I added zero in coin[0]). And then we fill the fist row with T[0][j] = T[0][j-coins[1]]+1 coins.sort() for i in range(len(coins)): T.append([]) for j in range(amount+1): T[i].append(0) for j in range(1,amount+1): if j < coins[1]: T[0][j] = float("inf") T[0][0] = 0 for j in range(1,amount+1): if coins[1]
@BorisMediaProds
@BorisMediaProds 9 жыл бұрын
I love you, thank you for this tutorial!
@gauravlagad1360
@gauravlagad1360 9 жыл бұрын
Very Good Video! I am preparing for Interviews right now, and this is simply the best explanation I have got till now :)
@5k4k1dhtp
@5k4k1dhtp 9 жыл бұрын
Great video, very concise and understandable.
@Sushil2874
@Sushil2874 5 жыл бұрын
You explain very nicely..!! Thank you..!!
@subodhjain8249
@subodhjain8249 9 жыл бұрын
code for the lecture m=size of array n=sum :) int count( int S[], int m, int n ) { // table[i] will be storing the number of solutions for // value i. We need n+1 rows as the table is consturcted // in bottom up manner using the base case (n = 0) int table[n+1]; // Initialize all table values as 0 memset(table, 0, sizeof(table)); // Base case (If given value is 0) table[0] = 1; // Pick all coins one by one and update the table[] values // after the index greater than or equal to the value of the // picked coin for(int i=0; i
@ashleycole3054
@ashleycole3054 9 жыл бұрын
awesome video.. keep doing such a great job...
@shrutiawasthi34
@shrutiawasthi34 9 жыл бұрын
such a nice explanation .. Thanks tushar.. it helped a lot... Keep posting more .:)
@raghibmusarrat1471
@raghibmusarrat1471 9 жыл бұрын
You algorithm gives wrong answer for the following problem: COINS: 2,3,5 Sum: 7 Please tell how to tackle this. The answer it gives is 0
@ronakgupta7492
@ronakgupta7492 8 жыл бұрын
Hi Tushar by this approach i had to set the first row to Integer.MAX and then was able to run 120 test cases out of 180 in leetcode. I really liked the approch but test cases like [186,419,83,408] sum =6249 giving me 18 instead of 20 idk whats wrong as the code seems to work for every other cases. Please post here if you know where i could be wrong
@pragatitanwar1671
@pragatitanwar1671 5 жыл бұрын
Awesome video!!
@yanivgardi9028
@yanivgardi9028 9 жыл бұрын
thanks Tushar. This post is very helpful.
@no_love_368
@no_love_368 6 жыл бұрын
Great work there!
@velramesh05
@velramesh05 4 жыл бұрын
Good explanation sir, thank you.
@borisjurcaga
@borisjurcaga 5 жыл бұрын
Thank you. I solving very similar problem and this video was very helpful.
@mohammedsafwan4447
@mohammedsafwan4447 3 жыл бұрын
thanks bro very helpful :)
@VINODKollipara
@VINODKollipara 9 жыл бұрын
great lecture..Thank you for posting it
@deepdesai7859
@deepdesai7859 8 жыл бұрын
u made my exam.. 👍👍this is very help full...
@DarlingMySweet
@DarlingMySweet 6 жыл бұрын
I think of it like this: Say we want to put [2, 5, 3] into 7, and we've already figured out that there is 1 way to put [2, 5] into 7. To add 3 to the mix we just add the number of ways that [2, 5, 3] goes into 4. The logic is that since 7 - 3 = 4, any combos that have 3 in them will be with some coin mix that totals 4. So [2, 5] into 7 = 1 + [2, 5, 3] into 4 = 1, means that [2, 5, 3] into 7 = 2.
@dongyuchen8625
@dongyuchen8625 4 жыл бұрын
if there isn't $1 coin this wouldn't work right?
@DhirajKumar-jf1yu
@DhirajKumar-jf1yu 4 жыл бұрын
If you implement the same way what he explained, it won't work if there isn't $1, but he has implemented in different way in his GitHub link. Even I implemented my code same way he explained and it fails if it doesn't have $1.
@dongyuchen8625
@dongyuchen8625 4 жыл бұрын
@@DhirajKumar-jf1yu Thanks mate. I solved it by set the initial values in the table to [0,'inf','inf'...'inf'], only update the value when the index is bigger than the coin. When the coins can't match the total value the last element in the table would always be 'inf'. But Tushar's video do give us a great example.
@sadmansakib00
@sadmansakib00 5 жыл бұрын
What if we weren't given the coin 1 ?
@harikrishna6233
@harikrishna6233 4 жыл бұрын
Thanks a lot .. its very helpful
@sainathtallam9262
@sainathtallam9262 4 жыл бұрын
great tutorial
@kittyjain4682
@kittyjain4682 3 жыл бұрын
@Tushar Roy - You just started with the solution without explaining why ? It's not going to help anyone if we have to memorize it without knowing y.
@rushabhshah434
@rushabhshah434 5 жыл бұрын
There is an error in 2-3rd line it should be min( T(i-1)(j)+1,T(i)(j-coin(i) ) Here logic is ...let me explain by example of the 6 in above table..now filling matrics we've 2 possiblity to get desired value either 6 is included or not if 6 is not included then we copy above's i.e. here 5's answer for that value of i If 6 is included then we've to add other coins accordingly for obtainig the desired value.Now,we check wether this ans. With 6 is optimal or not by checking it with above ans. I.e. here 5's ans. Which is already optimal uptill that 5 for j Ty !
@kiranwali4133
@kiranwali4133 9 жыл бұрын
Thanks a Ton Tushar !!
@shivjeetsingh8666
@shivjeetsingh8666 9 жыл бұрын
Nice video lecture. please upload more concept of dynamic programming.I solved many problem of Hacker Rank. Thanku :)
@ankitdeshpandey836
@ankitdeshpandey836 7 жыл бұрын
Awesome tutorial
@riyankpatel9410
@riyankpatel9410 4 жыл бұрын
Great sir🙏
@BRBallin1
@BRBallin1 10 ай бұрын
What about examples where 1 isn't a coin denomination? Then there is no "top" value to use
@user-rqyjdhueklaq
@user-rqyjdhueklaq 8 жыл бұрын
sum = 14 number of coins 3 2 3 5 Your algorithm gives wrong solution
@youssifkhairy1725
@youssifkhairy1725 8 жыл бұрын
You should Sort Numbers First
@Kasa99
@Kasa99 7 жыл бұрын
no need to sort
@henryhardcore8584
@henryhardcore8584 5 жыл бұрын
This algorithm doesn't check if the sum is possible to add up. If we have coins 2, and sum = 10.
@saumya1857
@saumya1857 8 жыл бұрын
awsome explanation :)
@chandan_prabhakar
@chandan_prabhakar 5 жыл бұрын
nice video gautam gambhir
@esmamouine2422
@esmamouine2422 7 жыл бұрын
trop bien mr.tushar.
@sanjeevshukla3821
@sanjeevshukla3821 9 жыл бұрын
i think we need a 0th row containing all 0's. so that if coin denomination starts from a number which is greater then the sum. then copy old value from top.
@fahadmohdshahid4110
@fahadmohdshahid4110 8 жыл бұрын
We need to fill it by infinite(int_max) not by 0.
@shobhitranjan3957
@shobhitranjan3957 3 жыл бұрын
The solution won't work if no denominations are possible.
@ShashankGupta_Shanki
@ShashankGupta_Shanki 9 жыл бұрын
great explanation...:)
@boosan_m
@boosan_m 5 жыл бұрын
Am I missing something? 1. This solution seems to work only for coins with 1 in it [2,3,5] & 11 won't work 2. Github link has a different solution in it.
@atulmaini1132
@atulmaini1132 9 жыл бұрын
how is the condition if a particular sum cannot be made with current coins in hand?eg if the min value of the coin present is 2 and sum is 3.
@hankesteveo
@hankesteveo 9 жыл бұрын
I kept up for a minute. Very good.
@DeepakSharma-di2fd
@DeepakSharma-di2fd 9 жыл бұрын
simply awesome
@anshumansrivastava8108
@anshumansrivastava8108 5 жыл бұрын
how to check here if the total sum is possible or not using denominations?
@nishanksoni7120
@nishanksoni7120 5 жыл бұрын
You never explained why this logic is working. And why we are going for DP and what dp approach we are using
@mymislife
@mymislife 5 жыл бұрын
Helpful! Thank you!!
@arunabhhejib1136
@arunabhhejib1136 4 жыл бұрын
Every time I see your video I get the solution but have no idea how to get there. It would be better if you start from recursion and then lead to finding an optimal substructure and so on.
@glennpavel4800
@glennpavel4800 4 жыл бұрын
hi, idk if I understand wrong from 1 or position 6 value 5 , five positions back is 0 , like this 1-1 pos =4, -1 pos = 3 ... -5 = 0 not 1 has you show in minute 3:20 , or I'm doing something bad? thanks for your help
@ihashib
@ihashib 5 жыл бұрын
Traced your code with the matrix you made, have a little problem, at min{T[i-1][j], 1+ T[i][j-coin[i]}, gonna consider that the 2d matrix has 0 as dummy index, so min{0, 1+ (0-1)}, so min will select, 0. So, T[1][1] is 0 not 1. If i don't consider 0 as dummy index, then it'll give error. Confused.
@parthhingu1525
@parthhingu1525 7 жыл бұрын
excellent teaching😊
@ivandrofly
@ivandrofly 9 ай бұрын
Finds min number of coin you will give as a change for example
@manojpandey7517
@manojpandey7517 4 жыл бұрын
He says "yes we will use dynamic programming..." and directly jumps to fill the table out of nowhere. That's why I could never learn anything from him. A bad technique to teach DP. The first and foremost step must be finding the recursive solution. I don't know why a lot of people out there recommend Tushar's video. I tried to watch his videos with a fresh mind but every time I felt let down. Sorry, but I cannot say "awesome tutorial" if I didn't learn anything. This is the last attempt I'm here.
@tricktreat7941
@tricktreat7941 2 жыл бұрын
His videos are not for people like you. Tushar is the smartest LeetCoder on KZbin. You need to reach a certain level before you can make use of his contents. When you do, you will realize why people recommend him.
@manojpandey7517
@manojpandey7517 2 жыл бұрын
@@tricktreat7941 it isn't about being smartest. He's teaching and a teacher doesn't need to be smartest to teach well. All I'm saying that this isn't a good way of teaching dynamic programming. If you want to know what a better way can be, watch Aditya Verma's dp Playlist.
@manojpandey7517
@manojpandey7517 2 жыл бұрын
Another user has also pointed out and suggested him to first explain what's tempting him to use dp.
@ucanhly1166
@ucanhly1166 2 жыл бұрын
I think the best teacher of algorithm on yt is Abdul Bari but actually haven't a lot of videos of dp but I learn from him so much and I came here to understand how to fill the table =))
@yudiguzman8926
@yudiguzman8926 6 жыл бұрын
Great. Thank so much.
@vasanthanv6143
@vasanthanv6143 2 жыл бұрын
Hi , Great video , but can you please tell me why are we taking Integer.MAX_VALUE -1 for intialization in the first row and why not Integer.MAX_VALUE ???
@omarosmanov727
@omarosmanov727 5 жыл бұрын
How to actually print out all combinations of amount? Let us say for amount=4, coins=[1,2,3] need to print out: (1,1,1,1); (2,2); (1,1,2);(1,3)
@TheNayanava
@TheNayanava 5 жыл бұрын
that should be fairly easy. just run a dfs. create a List> as your result. add the coin to the list and subtract that amount from the list before calling dfs recursively. once you hit 0, you can append that list to your result. Don't forget to do a res.add(new ArrayList(list)). else it will keep pointing to the same list. Now while backtracking you can remove that coin from the list and try adding a new coin as long as the value of the coin is not greater than the amount. Please see this solution will actually have O(n^n) time complexity.
@sheenamjindal6703
@sheenamjindal6703 3 жыл бұрын
wondering if it really solves the problem by writing the core logic you provided in the video. I tried but it didnt work for me,
@yanlingyang256
@yanlingyang256 7 жыл бұрын
how could you come up with such equation i wonder : )
@jg8742
@jg8742 5 жыл бұрын
dear sir . it would be helpful If you explain the problem with code also. As it is very difficult to code, although conceptually it is easy I was looking for an explaination like your trees playlist
@MayankSingh_is_me
@MayankSingh_is_me 4 жыл бұрын
for coins = [10, 50, 100, 200, 500, 1000] and total = 11, noobs wud get stucked. having coin of denomination "1" in the very beginning takes away some critical aspect making everything a happy case! @TusharRoy: can u comment?
@HarshaVardhan-kq1sv
@HarshaVardhan-kq1sv 4 жыл бұрын
Excellent sir. I have a doubt sir if we have multiple solution then how sir. Ex - coins: 1 2 3 Total: 4 Then dp tables will be 0 1 2 3 4 1 0 1 2 3 4 2 0 1 1 2 2 3 0 1 1 1 2 As dp[3][4] is 2, where the 2 is coming from above one as well as the 2 is coming from dp[3][1] i.e (1,3) and (2,2) So how we can traverse as we have multiple solutions like this. Sir can you explain this please sir.
@rahuldevgupta4513
@rahuldevgupta4513 3 жыл бұрын
It feels as if I am cramming the solution
@allanmagalhaes8211
@allanmagalhaes8211 3 жыл бұрын
lol i guess i understood everything until the final minute when you ran coding with no pauses
@johnhammer8668
@johnhammer8668 2 жыл бұрын
3:16 why go back coin value steps back? Why does this work?
@krishnamali6693
@krishnamali6693 8 жыл бұрын
Hello Tushar, Thanks for helping us all. I tried this algoritham on {2,3,5,7} set of coins with any total weight, it fails. So it is must to have coin of 1 in our set to get correct answer through this algo ?
@sahildhawan22
@sahildhawan22 8 жыл бұрын
Plain awesome! Just 1 question: Here for 11, I understood until the part where we'll require 2 coins, but couldn't understand the steps that led to determining that the coins were of denomination of 5 and 6.
@vinzane1412
@vinzane1412 4 жыл бұрын
Since the last element, 2 (number of coins) has originated from the previous row and the coin[i-1] is 6, it is clear that the solution contains 6 and so traverse back to 6 steps now and you find 1 which has come from it's prev row which is 5, so we also have 5.
@punitivetoe1819
@punitivetoe1819 3 жыл бұрын
How did row for 6 denom val 11 come to 2? Ans. For each cell we want min. Until now min for that denomination is the [i-1][j] val ie that value could have been made by using denominations until i ie 6 excluding 6. Remember each row represents cumulative factor ie considering all coins until i not just i. So using denom 1,5 we can make 11 using 3 coins. Thats the first term of the min i-1,j The question now remains can u make up 11 including 6. If u include 6, remaining value is 11-6 ie 5. So copy the value of min coins using 6 and reaching val 5 which is [i][j-denom ie 6-]
@MiguelCarvajalIB14
@MiguelCarvajalIB14 4 жыл бұрын
You got column and rows reversed, it is a bit confusing...
@mingleizha8868
@mingleizha8868 9 жыл бұрын
It is knapsack problem except you are trying to get the number of items!
@aziz9488
@aziz9488 8 жыл бұрын
True, it's the same as the unbounded knapsack problem
@mohammad-sm3zt
@mohammad-sm3zt 5 жыл бұрын
thanks a lot Tushar
@marcello-6351
@marcello-6351 9 жыл бұрын
Kudos Tushar! How would you adapt the solution to a real case scenario where coin supply is not infinite? Do you have anything about it? Thanks!
@dilipdiwakars
@dilipdiwakars 8 жыл бұрын
for a coin 5 , why you are saying we have to go back 5 steps back and add 1 to it. Can you please explain. I am not getting that part
@diegogusava
@diegogusava 8 жыл бұрын
Thanks Tushar!!!
@divyaanshu7387
@divyaanshu7387 9 жыл бұрын
In first column you wrote 0.But in the another example you used 1.What is correct way.And that should be 1 beaoz there is possibility not to give change.
@gunjanbarhaiya297
@gunjanbarhaiya297 6 жыл бұрын
Sir the trick you give to deal with dynamic programming is superb! But i just wanna know how can i develop the logic or trick as you do?
@aditya234567
@aditya234567 4 жыл бұрын
practice!
@pbdagr
@pbdagr 9 жыл бұрын
U r awesome in problem solving
@prinzmukka
@prinzmukka 9 жыл бұрын
Good one !!
Coin Change - Dynamic Programming Bottom Up - Leetcode 322
19:23
The Change Making Problem - Fewest Coins To Make Change Dynamic Programming
23:12
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
4.3 Matrix Chain Multiplication - Dynamic Programming
23:00
Abdul Bari
Рет қаралды 1,8 МЛН
L10. Minimum number of platforms required in a railway station
18:10
take U forward
Рет қаралды 51 М.
Longest Increasing Subsequence
7:09
Tushar Roy - Coding Made Simple
Рет қаралды 450 М.
DP#3 : Change Problem-Minimum number of coins Dynamic Programming
27:13
Jenny's Lectures CS IT
Рет қаралды 263 М.
How a Russian student invented a faster multiplication method
18:48
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 304 М.
How to Remember Everything You Read
26:12
Justin Sung
Рет қаралды 3,5 МЛН
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН