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

Поділитися
Вставка
  • Опубліковано 21 гру 2024
  • Lecture Notes/C++/Java Codes: takeuforward.o...
    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/take...
    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.o...
    You can also get in touch with me at my social handles: linktr.ee/take...

КОМЕНТАРІ • 571

  • @takeUforward
    @takeUforward  2 роки тому +100

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

    • @shashankreddy4620
      @shashankreddy4620 Рік тому +1

      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

    • @priyanshkumar17
      @priyanshkumar17 5 місяців тому

      undderstood

  • @_Kunal_Pawar
    @_Kunal_Pawar 10 місяців тому +13

    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 10 місяців тому +6

    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

  • @sirajkhan0825
    @sirajkhan0825 2 роки тому +82

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

  • @vatsalnanda4868
    @vatsalnanda4868 9 місяців тому +7

    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 6 місяців тому +1

      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!

  • @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 ❤

  • @shubhamraj25
    @shubhamraj25 Рік тому +35

    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

  • @TheWierdVibe
    @TheWierdVibe 2 роки тому +77

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

  • @Himanshhhhxu
    @Himanshhhhxu 3 місяці тому +2

    3 months ago I gave up on dp after watching 20 videos, but now I solved this problem on my own from bruth forces to space optimisation ❤❤🔥 you are legend brooo

  • @sayandey2492
    @sayandey2492 Рік тому +27

    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 ✅

  • @amitkumaryadav1365
    @amitkumaryadav1365 2 роки тому +38

    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 Рік тому +1

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

  • @akshayaashok8794
    @akshayaashok8794 5 місяців тому

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

  • @alifarah9
    @alifarah9 2 роки тому +19

    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

  • @pushkarraj4640
    @pushkarraj4640 2 роки тому +6

    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 👏👏

  • @SYCOA12CHAITANYAASOLE
    @SYCOA12CHAITANYAASOLE 4 місяці тому

    Undetsood !! Solved by myself and then watched the video . Thanks Striver.

  • @GManiKarthik
    @GManiKarthik 20 днів тому

    Solved this problem on my own by using concepts of DP-18 Coin Change - 1.
    Still understood all : - Recursion, Memoization, Tabulation & Space Optimization
    #Understood #Hatsoff #Striver

  • @vinaylj
    @vinaylj 5 місяців тому

    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.

  • @stith_pragya
    @stith_pragya 11 місяців тому +1

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

  • @DivyaKumari179
    @DivyaKumari179 6 місяців тому +1

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

  • @satendra6200
    @satendra6200 Рік тому

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

  • @janhvisingh-ry8wp
    @janhvisingh-ry8wp 6 місяців тому

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

  • @lapimpale
    @lapimpale 2 роки тому +5

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

  • @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

  • @kunjgupta1258
    @kunjgupta1258 Місяць тому +1

    Great video! But we dont actually need 2 vectors prev and curr. Only one is enough. This is my approach:
    long countWaysToMakeChange(int *d, int n, int value)
    {
    vectordp(value+1,1);
    int i,j;
    for(i=1;i

  • @ayushidalal5488
    @ayushidalal5488 Рік тому +1

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

  • @aineshsinha2831
    @aineshsinha2831 3 місяці тому

    Understood, great content as always by Striver! ❤

  • @anonymousboy2868
    @anonymousboy2868 6 місяців тому

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

  • @rajharsh3739
    @rajharsh3739 Рік тому

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

  • @VinayKumar-ze2ww
    @VinayKumar-ze2ww 2 роки тому +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;

  • @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.

  • @suyashjain3223
    @suyashjain3223 Рік тому

    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 🔥🔥🔥

  • @syedaqib2912
    @syedaqib2912 2 роки тому +13

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

  • @SethuIyer95
    @SethuIyer95 8 місяців тому

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

  • @dhanashreegodase4445
    @dhanashreegodase4445 11 місяців тому

    you are the real gem....thanks man

  • @dennyage4791
    @dennyage4791 2 роки тому +7

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

  • @aryansinha1818
    @aryansinha1818 2 роки тому +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 Рік тому

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

  • @prabhakaran5542
    @prabhakaran5542 9 місяців тому +1

    Understood ❤

  • @WasimKhan-fd1ub
    @WasimKhan-fd1ub Рік тому

    solved the problem before watching the video..✌✌

  • @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 2 роки тому

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

  • @princekoshti7245
    @princekoshti7245 Рік тому +1

    Solved by my own !! yeeeeehhh

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh Рік тому

    Understood Perfectly 🎆🔥🔥🔥

    • @aakashbhandari9761
      @aakashbhandari9761 Рік тому

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

  • @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.

  • @abhinavkumar7730
    @abhinavkumar7730 Рік тому

    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

  • @Abhigyan103
    @Abhigyan103 4 місяці тому

    Dont we have to re initialize cur at the begining of the outer loop ?
    21:36

  • @sohamsarkar5730
    @sohamsarkar5730 3 місяці тому +1

    Can we add a base case where
    If(target==0)return 1;

  • @avisoft-l2p
    @avisoft-l2p 5 місяців тому

    understood saar! amazing teacher you aareee!!!!

  • @subhamengine1143
    @subhamengine1143 2 роки тому +1

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

  • @ritikshandilya7075
    @ritikshandilya7075 6 місяців тому

    Thankyou so much Striver

  • @dheerajshukla7008
    @dheerajshukla7008 6 місяців тому

    understand sir thankyou so much for this amazing content its priceless

  • @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.

  • @ayushichoudhary1019
    @ayushichoudhary1019 9 місяців тому +1

    Could solve this one on my own!

  • @MukeshKumar-cc3uh
    @MukeshKumar-cc3uh 9 місяців тому

    Thank You and Understood ❤.

  • @theoopsguy
    @theoopsguy Рік тому

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

  • @ntgrn-pr5yx
    @ntgrn-pr5yx Місяць тому

    UNDERSTOOD THNX STRIVERRR

  • @pulkitchausali1354
    @pulkitchausali1354 2 роки тому +2

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

  • @rajnishtripathi5584
    @rajnishtripathi5584 Рік тому

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

  • @manthenanagendra1077
    @manthenanagendra1077 2 роки тому +1

    just loving this series,getting lots of confidence

  • @DevashishJose
    @DevashishJose 11 місяців тому

    Understood Thank you so much.

  • @rony-vn7ep
    @rony-vn7ep 2 дні тому

    why do we write separate condition for index ==0 ?

  • @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

    • @AquaRegia-i3u
      @AquaRegia-i3u Рік тому +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.

  • @AdityaPratapSingh-mb6jv
    @AdityaPratapSingh-mb6jv 4 місяці тому +3

    @takeUforward @everyone Can someone explains what the difference between the problem statement of DP 20 and 22? Because i think both the problem are same.

    • @spexled
      @spexled 2 місяці тому

      here we are counting the total number of ways.....in dp 20 we are counting the minimum number of coins the code is similar only just the return values are different

  • @KrishnaGuptaTech
    @KrishnaGuptaTech 13 днів тому

    UNDERSTOOD !!!

  • @shreshthakochgaway5403
    @shreshthakochgaway5403 2 роки тому +2

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

  • @pratikshadhole6694
    @pratikshadhole6694 Рік тому +1

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

  • @sameersahu4569
    @sameersahu4569 2 роки тому

    Understood!!!Thank you for inflicting such confidence

  • @adarshupadhyay8265
    @adarshupadhyay8265 2 роки тому

    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

  • @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 🙏 !!

  • @shubh625
    @shubh625 5 місяців тому

    understood did all on my own

  • @ranasauravsingh
    @ranasauravsingh 2 роки тому +1

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

  • @reverbify8692
    @reverbify8692 Рік тому +1

    Should we include a base condition of target==0 and then return 1,

    • @himanshubarak3528
      @himanshubarak3528 Рік тому +1

      i have the same doubt i included it and it worked for me

  • @vedantmahajan4922
    @vedantmahajan4922 10 місяців тому +2

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

    • @VinayKumar-xs6el
      @VinayKumar-xs6el 3 місяці тому

      along with target < 0 || i == -1 return 0 will work fine

  • @uditgarg6508
    @uditgarg6508 7 місяців тому +1

    I have a doubt . upon entering a test case [1, 2 , 5] and amount 0, i am getting the expected output in leetocode as 1. How is this possible? Is not taking any coin also counted as a combination?

    • @ankitris5201
      @ankitris5201 6 місяців тому

      in the strivers code he didn't assume the case amount == 0 condition . In leetcode we have to consider the case when amount == 0 and return 1;

  • @dhruvkaran9724
    @dhruvkaran9724 2 роки тому

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

  • @sushmitagoswami2033
    @sushmitagoswami2033 2 роки тому +2

    I have one doubt, how can we return boolean from a function which returns long?

    • @dharamshalatrip-cn8rf
      @dharamshalatrip-cn8rf Рік тому

      prev and curr array are also long type in first for loop wherever condition satisfies ,prev will store value 1(as in true==1 & false =0)

  • @VishalSingh-tj1nk
    @VishalSingh-tj1nk 2 роки тому

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

  • @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]

  • @parthsalat
    @parthsalat 2 роки тому +1

    Recursion pseudocode: 10:30

  • @aryansinha1818
    @aryansinha1818 2 роки тому

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

  • @ss-lk2rs
    @ss-lk2rs Рік тому +1

    Why didn't we add 1 in the take case?

  • @aps8874
    @aps8874 5 місяців тому

    Thank you so much!

  • @arihantjammar8888
    @arihantjammar8888 Рік тому

    Understood 😊

  • @anshul5533
    @anshul5533 2 роки тому +5

    Striver Bhaiya again i think we can also optimize it in terms of space by using just an single array

  • @ishaansharma7140
    @ishaansharma7140 Рік тому

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

  • @jaydevkundu554
    @jaydevkundu554 Рік тому

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

  • @abudanish196
    @abudanish196 Рік тому

    Understood!

  • @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

  • @harinimurali1180
    @harinimurali1180 5 місяців тому

    Why are we not adding +1 when we pick the coin?

  • @shivkumar-og4ow
    @shivkumar-og4ow 2 роки тому

    uderstood ! easy to understand the way of your explanation.

  • @sakshamgarg1298
    @sakshamgarg1298 3 місяці тому

    Sir what is int *a how is that a array

  • @Paul_atredis
    @Paul_atredis 2 роки тому

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

  • @devankgupta8497
    @devankgupta8497 5 місяців тому

    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

    • @animeshyadav8187
      @animeshyadav8187 2 місяці тому

      int tk = 0;
      if(target>=coins[ind]){
      int tk = minCoins(ind-1, target - coins[ind], dp, coins);
      }
      we are using the case where only and only if the target is greater than the current coin in the array then the take part will work otherwise it will simply not run the will take recursion

  • @Plams5
    @Plams5 Рік тому

    Great Stuff.

  • @AnkitKumar-nz1hs
    @AnkitKumar-nz1hs Місяць тому

    yes i understood

  • @rakeshkumaryadav1117
    @rakeshkumaryadav1117 5 місяців тому

    I think one more base case needed
    If (target==0)
    Return 1;
    Even though it can be achieved by
    Function of not take but this base case can be added
    Please correct me if I am wrong

  • @santoshb7776
    @santoshb7776 Рік тому

    Understood sir.

  • @aakankshajain8988
    @aakankshajain8988 Рік тому

    in the base case is T is divisible by arr[0] then we should return T/arr[0 na not 1??
    |

  • @studynewthings1727
    @studynewthings1727 8 місяців тому

    Understood all the four methods 1) Recursion, 2) Memoization, 3) Tabulation, 4) Space Optimization

  • @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

  • @deepakbasoiya975
    @deepakbasoiya975 2 роки тому

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

  • @saumilaggarwal5233
    @saumilaggarwal5233 Рік тому

    thnx i did this on my own