DP 22. Coin Change 2 | Infinite Supply Problems | DP on Subsequences

Поделиться
HTML-код
  • Опубликовано: 15 фев 2022
  • Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
    Problem Link: bit.ly/33Kd8o2
    Pre-req for this Series: • Re 1. Introduction to ...
    a
    Make sure to join our telegram group for discussions: linktr.ee/takeUforward
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we solve the problem of ways to form Coin Change. We start with memoization, then tabulation, then two-row space optimization. This problem is the eighth problem on DP on Subsequences Pattern. Please watch DP 14 before watching this.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.org/interviews/s...
    You can also get in touch with me at my social handles: linktr.ee/takeUforward

Комментарии • 518

  • @takeUforward
    @takeUforward  2 года назад +87

    This problem can also be solved using 1D array, we have discussed about it in DP 23.

    • @shashankreddy4620
      @shashankreddy4620 Год назад

      int change(int amount, vector& coins) {
      vector dp(amount+1, 0);
      dp[0]=1;
      for(int &coin: coins) {
      for(int i=coin; i

    • @de_coder1
      @de_coder1 Год назад

      Can't we add the case when value==0
      And fill the first column of dp array with 1

    • @priyanshkumariitd
      @priyanshkumariitd Месяц назад

      undderstood

  • @_Kunal_Pawar
    @_Kunal_Pawar 5 месяцев назад +7

    Understood! Since you've already explained these concepts so well in the previous problem, I was able to solve it on my own. Thanks, Striver! 💯

  • @fomoCoder69
    @fomoCoder69 5 месяцев назад +5

    Have been following your dp series and was able to solve the problem by my own successfully in a single try, thanks a lot for the efforts you took to provide such a great dp series

  • @prashantgupta6093
    @prashantgupta6093 2 года назад +15

    I tried this on my own and I was super happy to solve this problem with all the methods. Thanks, Brother ❤

  • @TheWierdVibe
    @TheWierdVibe 2 года назад +66

    I think the consequence of this DP series will be free education and no college😃

  • @shubhamraj25
    @shubhamraj25 Год назад +28

    ths was very same to coin Change 1, just instead of returning minimum, we need to return 1 if path exists or 0 if does not

  • @akshayaashok8794
    @akshayaashok8794 15 дней назад

    I was able to do this question on my own even before watching this video. Thank you so much for this wonderful playlist.

  • @SethuIyer95
    @SethuIyer95 3 месяца назад

    Thank you so much for your DSA playlist, my FAANG interviews are going butter smooth xD

  • @anonymousboy2868
    @anonymousboy2868 Месяц назад

    was Able to think of complete solution the moment you explained question.Big Thanks

  • @sirajkhan0825
    @sirajkhan0825 2 года назад +74

    Was able to think of the solution even before he started explaining the recursive approach... Thanks to striver... Kudos to your effort...

    • @blurrybrush1010
      @blurrybrush1010 2 года назад +1

      same

    • @ARSHADKHAN-hc6pb
      @ARSHADKHAN-hc6pb 2 года назад +1

      land

    • @abhilash4976
      @abhilash4976 2 года назад +6

      i have a doubt, why didnt he take a base case with target == 0

    • @GandharveSolanki
      @GandharveSolanki 2 года назад +1

      @@abhilash4976 bro it is considered at time you reach index 0

    • @armed3719
      @armed3719 Год назад

      @@abhilash4976 did you found out why?

  • @ayushidalal5488
    @ayushidalal5488 Год назад +1

    Following the entire series and was able to do this question on my own without watching the video! Thankyou so much :)

  • @DivyaKumari179
    @DivyaKumari179 Месяц назад +1

    UNDERSTOOD sir...
    Thanku so much for this amazing series.

  • @vatsalnanda4868
    @vatsalnanda4868 4 месяца назад +3

    Hi everyone. Thanks a lot Striver for making DP so easy and interesting for all of us. Just had one small query (might sound trivial 😅)- In this problem, amount and ind are changing params and according to recursion, we have to write the base cases for all the changing parameters. Why don't we handle the case of amount==0? As if amount==0, we can return 1 as we have achived 1 subsequence equal to the amount.
    Thanks in advance.

    • @r.apuroop4034
      @r.apuroop4034 Месяц назад

      yeah bro, we need to take amount == 0 case as well. Leetcode throws an error if u dont take this case. Thanks for pointing it out!

  • @satendra6200
    @satendra6200 10 месяцев назад

    understood ! memoization, tabulation , space optimization all done without watching video

  • @stith_pragya
    @stith_pragya 7 месяцев назад +1

    UNDERSTOOD.......Thank You So Much for this wonderful video..............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @alifarah9
    @alifarah9 2 года назад +18

    Striver, may I suggest next videos? Word Break I & II. These problems have both recursive and DP solutions I think they would make excellent videos. Thanks

  • @vinaylj
    @vinaylj 23 дня назад

    I was able to do this problem without watching the solution. thanks a lot striver for teaching such tough topic for beginners in such easy way.

  • @dhanashreegodase4445
    @dhanashreegodase4445 6 месяцев назад

    you are the real gem....thanks man

  • @suyashjain3223
    @suyashjain3223 9 месяцев назад

    I am able to solve coin change 2,Unbounded Knapsack and Rod cutting problem with memoization ,tabulation and space optimization on my own without watching video. Thanks a lot Striver!! Surely the Best DP playlist 🔥🔥🔥

  • @sayandey2492
    @sayandey2492 11 месяцев назад +24

    Was able to successfully write both top-down and bottom-up code at once without even starting the video...Thanks striver for bringing me till this point...the journey will continue ✅

  • @sameersahu4569
    @sameersahu4569 2 года назад

    Understood!!!Thank you for inflicting such confidence

  • @user-yd9xf2ss3m
    @user-yd9xf2ss3m 16 дней назад

    understood saar! amazing teacher you aareee!!!!

  • @rajharsh3739
    @rajharsh3739 9 месяцев назад

    Understood striver bhaiya ✌ -- solve this problem before watching this video 💪💪

  • @DevashishJose
    @DevashishJose 7 месяцев назад

    Understood Thank you so much.

  • @pushkarraj4640
    @pushkarraj4640 2 года назад +5

    Just a humble request please please please don't stop making the videos after you would have left India ... Now can confidently solve any dp problem with all four approaches taught by u ... Simply amazing teaching by u bhaiya 👏👏

  • @janhvisingh-ry8wp
    @janhvisingh-ry8wp 2 месяца назад

    Its still hard to believe i can literally code on my own now after watching your series , never thought dp would be this easy !

  • @ritikshandilya7075
    @ritikshandilya7075 Месяц назад

    Thankyou so much Striver

  • @theoopsguy
    @theoopsguy Год назад

    I solved this problem on my own. Thanks striver. You teach amazing.

  • @dheerajshukla7008
    @dheerajshukla7008 Месяц назад

    understand sir thankyou so much for this amazing content its priceless

  • @heshamalam7193
    @heshamalam7193 2 года назад +5

    came to know about this channel from my brother
    he is also doing CSE from jalpaiguri government collage... he also told u have done ur engineering from the same collage
    thank u very much sir

  • @WasimKhan-fd1ub
    @WasimKhan-fd1ub 7 месяцев назад

    solved the problem before watching the video..✌✌

  • @subhamengine1143
    @subhamengine1143 2 года назад +1

    watched the first 2 mins, and solved it myself.. Thanks bhaiya for training us so so well

  • @sumitkumar5559
    @sumitkumar5559 Год назад

    I solved this problem on my own with all methods, from the knowledge of previous videos.
    Thanks Striver

  • @MukeshKumar-cc3uh
    @MukeshKumar-cc3uh 4 месяца назад

    Thank You and Understood ❤.

  • @lapimpale
    @lapimpale 2 года назад +5

    Even though you are busy, you uploaded video for us. Great.!!

  • @VishalSingh-tj1nk
    @VishalSingh-tj1nk 2 года назад

    base case can be generalized if we go with array size rather than array index.

  • @manthenanagendra1077
    @manthenanagendra1077 Год назад +1

    just loving this series,getting lots of confidence

  • @aps8874
    @aps8874 15 дней назад

    Thank you so much!

  • @LowkeyCoder
    @LowkeyCoder 8 месяцев назад

    "Understood" and thank you!

  • @yashrawat5158
    @yashrawat5158 Год назад

    I am able to think of the solution before you start explaining the recursive approach .Thank you striver..without you it can't be possible.i owe you a big one.

  • @Plams5
    @Plams5 10 месяцев назад

    Great Stuff.

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh 8 месяцев назад

    Understood Perfectly 🎆🔥🔥🔥

    • @aakashbhandari9761
      @aakashbhandari9761 8 месяцев назад

      Ek doubt hai base case me mod operator true and false return krega na

  • @shubh625
    @shubh625 7 дней назад

    understood did all on my own

  • @syedaqib2912
    @syedaqib2912 2 года назад +11

    LOL, you are destroying all the jobs and platforms of paid content creators 😁😁
    Thanks for the video !!

  • @ranasauravsingh
    @ranasauravsingh 2 года назад +1

    UNDERSTOOD... !
    Thanks striver for the video... :)

  • @kunalsaha9239
    @kunalsaha9239 2 года назад +9

    Thanks a lot @Raj Bhaiya for reducing the fear of Dp.
    Now I can think of the intuition by my own.

  • @prabhakaran5542
    @prabhakaran5542 5 месяцев назад +1

    Understood ❤

  • @ayushichoudhary1019
    @ayushichoudhary1019 4 месяца назад +1

    Could solve this one on my own!

  • @princekoshti7245
    @princekoshti7245 8 месяцев назад +1

    Solved by my own !! yeeeeehhh

  • @VinayKumar-ze2ww
    @VinayKumar-ze2ww Год назад +4

    If anyone didn't understand
    if(n==0) return k%arr[0]==0;
    Well, it means whether constantly decreasing the number by arr[0] will lead k to 0
    Or in place of this, you can write
    if(n==-1) return k==0;

  • @rajnishtripathi5584
    @rajnishtripathi5584 Год назад

    one of the best solution i have ever seen on youtube . thank you striver bhaiya

  • @UECAshutoshKumar
    @UECAshutoshKumar Месяц назад +1

    Understood!

  • @dhruvkaran9724
    @dhruvkaran9724 2 года назад

    now i am solving own and seeing videos to verify my approach ... thanks strive ... UNDERSTOOD

  • @arihantjammar8888
    @arihantjammar8888 9 месяцев назад

    Understood 😊

  • @aayushsharma1048
    @aayushsharma1048 2 года назад +1

    Thank you so much bhaiya for all the knowledge you are sharing with us.

  • @saumilaggarwal5233
    @saumilaggarwal5233 9 месяцев назад

    thnx i did this on my own

  • @jyothiyadav2595
    @jyothiyadav2595 9 месяцев назад

    Understood🎉

  • @santoshb7776
    @santoshb7776 8 месяцев назад

    Understood sir.

  • @gunduboinadileep9523
    @gunduboinadileep9523 8 месяцев назад

    understood!!

  • @nithishlelll9664
    @nithishlelll9664 7 месяцев назад

    Understood!!

  • @aruna5869
    @aruna5869 5 месяцев назад

    understood :)❤

  • @dennyage4791
    @dennyage4791 2 года назад +7

    This is best dp series ever ...🔥🔥🔥

  • @shashankdaksh7554
    @shashankdaksh7554 2 года назад

    Please upload notes for this and previous video . They are icing on the cake.

  • @user-tn3ni5cv1s
    @user-tn3ni5cv1s Год назад

    Was able to solve it without looking at the video....thank you striver😁

  • @Harshit126
    @Harshit126 2 года назад +1

    Understood, thanks
    Again, Similar to the previous time (DP 20) when we have to stay at same index in case of selection.
    In this scenario if we go for tabulation in reverse fashion, then it gives 1 in case of correct answer but gives correct answer in case of moving forward from 0.
    Could you please explain why does that happens or provide a thread which might help in understanding how to go about this

    • @user-ie8sy7wo4m
      @user-ie8sy7wo4m Год назад +1

      Its because when we are at same index (i.e. in case we take the element), we want to use previous indexes of array. for if you want to find dp[index][target]. you need dp[index][target-arr[index]], you want some value in same row but before current index.
      Now if you fill from reverse you haven't filled previous indexes and they are still zero, so you need to fill from starting.

  • @manikiran949
    @manikiran949 6 месяцев назад

    Understood :)

  • @amitkumaryadav1365
    @amitkumaryadav1365 Год назад +26

    guys , this concept are tough . if we not able to solve in one go ...relax even he is summiting multiple times ....hatsoff to the effort by the striver , he is helping alot .

    • @Area-fd8ht
      @Area-fd8ht Год назад

      Striver bhaiya pdaye toh sab aasan hai..

    • @Area-fd8ht
      @Area-fd8ht Год назад

      Wese amit tu electrical engineering kar rkha hai kisi jhatu college se..esliye tu jhatu hi hai

    • @pratik.784
      @pratik.784 Год назад

      Striver has submitted in its first attempt

    • @ChiragWadwa
      @ChiragWadwa 7 месяцев назад +1

      ​@@pratik.784Bhai wo candidate master h , he has a lot of experience

  • @aryansinha1818
    @aryansinha1818 Год назад +1

    Why do we take value + 1 here, whereas in some another video we took value only ? *vector dp(n,vector (value+1,-1))*

    • @srikarkale2351
      @srikarkale2351 9 месяцев назад

      because the values of the value variable range from 0 to value (both included)

  • @archerboomer9623
    @archerboomer9623 2 года назад +2

    How do it if we have to count Unordered ways instead of ordered ways?
    For example: {1,2,1} and {2,1,1} will be considered Different.
    You can Refer to Cses Coin combinations 1 for this problem.

  • @shivkumar-og4ow
    @shivkumar-og4ow Год назад

    uderstood ! easy to understand the way of your explanation.

  • @tg62672
    @tg62672 Год назад

    finally after 21 videos i was able to solve question using memoization as well as tabulation without any help.

  • @girishbhargava6367
    @girishbhargava6367 2 года назад +1

    Striver, in memoization approach, do we need to write this case if(value == 0) return 0 ; otherwise, the value will go negative very soon. The answer is correct both ways, but I think adding this to our code will make it much more efficient. I have pasted the memoization approach below:
    #include
    long long int helper(int* denominations , int index , int value , vector& dp){
    if(value < 0)
    return 0 ;
    if(value == 0)
    return 1 ;
    if(index == 0){
    if(value % denominations[index] == 0) return 1 ;
    else return 0 ;
    }
    if(dp[index][value] != -1)
    return dp[index][value] ;
    long long int notTake = helper(denominations , index - 1 , value , dp) ;
    long long int take = 0 ;
    if(denominations[index]

    • @ayushsarna1706
      @ayushsarna1706 Год назад

      why are we using value+1 in "vector dp(n , vector(value+1 , -1)) ; " ?

  • @Parthj426
    @Parthj426 Месяц назад

    UNDERSTOOD

  • @parthkandwal8343
    @parthkandwal8343 Год назад

    Understood Sir. Hats off to you for what you are doing.

  • @shreyraj2574
    @shreyraj2574 Год назад

    Try running the tabulation for :
    Tar = 9 and coins = {5,6,7,9,11,12,13,15}
    It fails !!
    Because , what I think is , we have missed the initialisation wherein the Tar = 0 can be achieved at all indices . So I added this line of code above the tabulation :
    for(int j=0; j>=n ; j++)
    dp[j][0] = 1 ;
    But , when I add this, all other test cases fail , so plz help me if anyone can 🙏 !!

  • @per.seus._
    @per.seus._ 25 дней назад

    understoood

  • @yashwanthsai762
    @yashwanthsai762 6 месяцев назад

    can someone explain me base case in tabular? how to do that and what it does

  • @vashishthsharma4411
    @vashishthsharma4411 10 месяцев назад +1

    understood

  • @anime__ash
    @anime__ash Год назад

    Understood and did all the code by myself !

  • @aryansinha1818
    @aryansinha1818 Год назад

    How to know if we have to take *f(denominations, n-1,value);* n or n-1

  • @vikasgupta6701
    @vikasgupta6701 Год назад

    Able to solve using the concept of prev coin change 1 problem. Thanks

  • @vedantmahajan4922
    @vedantmahajan4922 6 месяцев назад +1

    we can write one more base case if(target == 0) return 1;

  • @kushjoshi9716
    @kushjoshi9716 Год назад

    UNDERSTOOD! and going forward everyday!

  • @pratikshadhole6694
    @pratikshadhole6694 Год назад +1

    you make me imagine recursion without writing it down.. GOAT❤

  • @aryannambiar5
    @aryannambiar5 Год назад

    If we consider the recursive part of the function where we dont decrease the index , in that case hw will my function reach the base case of index =0 since only amt will decrease and index will remain same hence may result in runtime error right? I dont understand hw that part is working

  • @devankgupta8497
    @devankgupta8497 15 дней назад

    could u please explain how did u take the base ase of (if (ind==0)) didnt really get that part
    cuz int he Take case we will never reach ind==0 so the recussion will keep continuing so cud u please explain that part

  • @harshjha6735
    @harshjha6735 9 месяцев назад

    Understood

  • @abhinavkumar7730
    @abhinavkumar7730 7 месяцев назад

    I have 3 questions:
    1) If we are counting total number of ways, would optimal substructure be applied?
    2) Is this the same problem statement as Leetcode's Combination Sum 1 and 2. If it is, there I didn't use dp, and they didn't ask to optimize.
    3) Instead of write that base case with two if statements, can we just write if(target < 0 || index

  • @pulkitchausali1354
    @pulkitchausali1354 Год назад +2

    "Solve this problem with my own without seeing the video my confidence and thinking skill are growing day by day thank you Striver"

  • @andrewcenteno3462
    @andrewcenteno3462 8 месяцев назад

    Why is there no base case where if (t==0) return 1; ?

  • @mik1430
    @mik1430 2 года назад

    small change is needed in tabulated solution for cases like target = 7 and array [3,7] where it'll fail . Amazing playlist btw:
    n = len(coins)
    dp = [[0 for _ in range(amount+1)] for _ in range(n+1)]
    #1 way to select 0 amount
    for j in range(n):
    dp[j][0] = 1
    for index in range(n-1,-1,-1):
    for remAmount in range(1,amount+1):
    res = dp[index+1][remAmount]
    if remAmount - coins[index] >= 0:
    res += dp[index][remAmount-coins[index]]
    dp[index][remAmount] = res
    return dp[0][amount]

  • @deepakbasoiya975
    @deepakbasoiya975 Год назад

    I think I am become addicted to yours dp video 😳🤯

  • @adarshupadhyay8265
    @adarshupadhyay8265 Год назад

    why are we using value+1 in "vector dp(n , vector(value+1 , -1)) ; " ?

    • @HimanshuGupta-ni3pk
      @HimanshuGupta-ni3pk Год назад

      making the 2d array ...see some of the pepcoding tabulation dp videos you will understand

  • @shreshthakochgaway5403
    @shreshthakochgaway5403 2 года назад +1

    Why not adding a plus one here too in the take case?

  • @jaydevkundu554
    @jaydevkundu554 Год назад

    Understood brother..
    solved before seeing the video..
    It's only because of u striver..🔥🔥

  • @ratinderpalsingh5909
    @ratinderpalsingh5909 2 года назад

    Understood, sir. Thank you very much.

  • @siddharthmishra8233
    @siddharthmishra8233 2 года назад

    @take_U_forward , i am not able to understand tabulation till now , I have watched all last 21 videos but still finding difficult to do tabulation

  • @ishaansharma7140
    @ishaansharma7140 Год назад

    why we didnt consider the base case case where target ==0 ?

  • @gaurabdas6503
    @gaurabdas6503 2 года назад

    Understood!
    Using single array solution -
    long countWaysToMakeChange(int* deno, int n, int tar)
    {
    vector prev(tar+1);
    for(int j=0 ; j

  • @kashyapvishvas
    @kashyapvishvas 2 года назад

    Thanks for DP playlist