Coin Change Problem (Recursion)

Поділитися
Вставка
  • Опубліковано 20 січ 2025

КОМЕНТАРІ • 65

  • @ShaunakDe
    @ShaunakDe 7 років тому +53

    I love the fact that you solved the problem likes some one attempting it for the first time and showed the error one might run into. That made it intuitive to follow. Please keep making these!

    • @alokgupta1226
      @alokgupta1226 7 років тому +3

      +1 on this. My code is EXACT same as the one at ~5min and I've been wondering why I'm unable to get the right solution. Awesome job for guiding us through all the way!

    • @Beyondhumanlimits1
      @Beyondhumanlimits1 6 років тому

      I found the opposite.

  • @jguno2744
    @jguno2744 6 років тому +6

    I like the way you describe how to count permutations and combinations each. Very helpful

  • @RanyElHousieny
    @RanyElHousieny 7 років тому +7

    This is the best explanation to the Coin Exchange problem, better than Gayle's explanation. I like the simple way you drove the solution

  • @pratikjain4704
    @pratikjain4704 6 років тому +6

    Really loved the solution, especially how you made some mistakes at first purposely and then solved them. You rock \m/

  • @calvinchankf
    @calvinchankf 6 років тому +1

    this video is a lot easier to understand than your another one(dynamic programming approach). thanks bro

  • @bhavneeshrustagi1291
    @bhavneeshrustagi1291 4 роки тому

    Loved the initial code vs final code explaination. I was hitting the exact same issue. Very helpful indeed

  • @surajch2678
    @surajch2678 5 років тому

    I ran into same error when i tried to solve the problem. Keep making more of these. Thanks

  • @jevonkfm6550
    @jevonkfm6550 6 років тому

    The way you described the problem was very thorough! Keep up the good work!

  • @manasbansal7946
    @manasbansal7946 5 років тому +5

    how would we print out all the combinations?

  • @ArunManivannan
    @ArunManivannan 6 років тому +2

    Simply brilliant ! I was solving the DavisStaircase using the first part of the video. Thanks a lot !!

  • @leela_prasad
    @leela_prasad 4 роки тому

    superb explanation . you don't just explain but you make me to realize the mistakes,...... :)

  • @somyadeepshrivastava6745
    @somyadeepshrivastava6745 6 років тому

    Love the way you teach from a general solution that might end up in some exception to a perfect solution checking all possibilities, even I did the mistake of repeating combination but cleared after your watching your video.
    Please add up more videos regarding Dynamic problems and Greedy problems.

  • @ManishJain2
    @ManishJain2 3 роки тому

    Great solution! Please note this problem is different than LC 322. Coin Change (minimum # of coins for an amount)

  • @rohitrai4215
    @rohitrai4215 3 роки тому

    Wow Wow Wow. You deserve more subscribers.

  • @dhondipranav3721
    @dhondipranav3721 4 роки тому

    Why memorization not working in this Code?😟😦😦😦😦😦😦😦😟😦😦😟😟😟😟😦

  • @bluemanwelder
    @bluemanwelder 6 років тому +2

    liked that whiteboard helped out a lot.

  • @samriddhisingh9803
    @samriddhisingh9803 5 років тому

    I was attempting it in the same way,but got stuck when i had to remove the permutations.. and u helped me.. Thanks alot!!

  • @TheTessatje123
    @TheTessatje123 5 років тому +1

    Hi! The code explanation is super clear. Is it possible to explain how we can get the sets of possible combinations (not only that a combination exists, but the real numbers in the set)?

  • @DeepakYadav-jc8mo
    @DeepakYadav-jc8mo 2 роки тому

    intuitive explanation!

  • @soheil9248
    @soheil9248 4 роки тому

    Thanks.
    How would we print out all the combinations?

  • @freek633
    @freek633 5 років тому

    Wow youre really patient, nice!

  • @michaeldausmann6066
    @michaeldausmann6066 6 років тому

    thanks for taking the time to explain this. nicely done.

  • @brimmedHat
    @brimmedHat 3 роки тому

    This helped me a lot, thanks.

  • @nenasaleem924
    @nenasaleem924 5 років тому

    Why do we need the static value amount if we're not going to use it?

  • @jeremyxu7646
    @jeremyxu7646 6 років тому +3

    I think the second solution has an issue of iteration. the 'coin' in the loop is adding up as the index which should be loop thorough as the elements in the coins array. Since your coins array is [1, 2] which match the index, the solution will work, but if your coins array is [1, 2, 5] the solution will fail!

  • @hanha014
    @hanha014 5 років тому

    Thanks for a great video. I did an alternative implementation in Scala without any assignment/for loop to make it even more functional-style. The proposed solution in the video might be more efficient though since I haven't figured out if it is possible to make this approach tail recursive somehow. (click on the show more link to see code)
    def countChange(money: Int, coins: List[Int]): Int =
    if (money < 0) 0
    else if (money == 0) 1
    else coins match {
    case Nil => 0
    case head :: tail => countChange(money - head, coins) + countChange(money, tail)
    }

  • @krood_
    @krood_ 5 років тому

    Great Explanation! Loved it

  • @bhaskargoud893
    @bhaskargoud893 3 роки тому

    excellent explanation

  • @jmdoii-c8i
    @jmdoii-c8i 5 років тому

    In your recursive solution that didn't take into account a current coin, why did your recursive solution 3 - 2 = 1 iterate back to 1 - 1 again?isn't that outside the for loop?

  • @julianbullmagic
    @julianbullmagic 4 роки тому

    this is very smooth

  • @zainmaqsood5070
    @zainmaqsood5070 6 років тому

    How would you print the combinations along with the total no of combinations?

  • @Marco.12221
    @Marco.12221 5 років тому

    thank you for the clear explanation, really helpful

  • @omarosmanov727
    @omarosmanov727 5 років тому +2

    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)

  • @PavitraAbhiram
    @PavitraAbhiram 7 років тому +2

    i loved the way it is explained. Simple and easy to follow. What is the time complexity of this solution?

    • @cl3375
      @cl3375 7 років тому

      lol recurrence relations are not fun! recurrence relations are difficult enough with one parameter [but...T(0,m)= c1, T( 1 sounds like a terrible start haha]

  • @nenasaleem924
    @nenasaleem924 5 років тому

    What's the time complexity for this program??

  • @NANDINIGOEL
    @NANDINIGOEL 6 років тому

    how about calculating time and space complexity

  • @AA-tm3ew
    @AA-tm3ew 3 роки тому

    nice explanation!

  • @NaserMohdBaig
    @NaserMohdBaig 4 роки тому +2

    Hi Neil,
    Thank you for the amazing explanation! I had a question though. How do we attempt the same with memoization? I tried with *memoization* and it returns *all the combinations with permutations* instead of just the unique combinations.
    Here's my code if you or anybody could help me find any error:
    function coinChange(denominations, value, coin, memo=[]){
    if (memo[value]) return memo[value];
    if (value < 0) return 0;
    if (value === 0) {
    return 1;
    }
    let amount = 0;
    for (let x = coin; x < denominations.length; x++) {
    amount += coinChange(denominations, value-denominations[x], x, memo)
    }
    return memo[value] = amount;
    }
    coinChange([1, 5, 10, 25], 6, 0) //returns 3 instead of 2

  • @johnmassaad7904
    @johnmassaad7904 5 років тому

    Thank you very much!! Great explanation!!!

  • @getsuha
    @getsuha 5 років тому

    Fantastic explanation...

  • @karthikragunathanandakumar8834
    @karthikragunathanandakumar8834 7 років тому

    Great way of explaining

  • @dottiejiang9317
    @dottiejiang9317 6 років тому

    should we use a memo?

  • @ЛевПоляков-д4э
    @ЛевПоляков-д4э 6 років тому

    What is the time of this code, O()?

  • @corymaklin7864
    @corymaklin7864 3 роки тому

    You're amazing

  • @Jahaniam
    @Jahaniam 7 років тому +2

    Grea videot! Keep it up!

    • @Jahaniam
      @Jahaniam 7 років тому

      That would be great if you add memoization to it aswell. you need to save: Mem[(money,index)] into your table

  • @baidya87
    @baidya87 4 роки тому

    so helpful!

  • @oceanview-u8q
    @oceanview-u8q 6 років тому

    print combo along with number of combo...
    coins = [1,2,3]
    temp = []
    def combo(amount, current_coin, temp):
    if amount == 0:
    print("change is 0 ", temp)
    return 1
    if amount < 0:
    return 0
    nCombos = 0
    for i in range(current_coin, len(coins)):
    temp.append(coins[i])
    nCombos += combo(amount-coins[i], i, temp)
    temp.pop()
    return nCombos
    print("number of combos : ", combo(4, 0, temp))

  • @freshjawn4962
    @freshjawn4962 6 років тому

    very helpful !

  • @malharjajoo7393
    @malharjajoo7393 6 років тому

    Nice explanation, but this method won't work when one of the coin denominations is not a multiple of another.
    In this case, "1" is always among the coins and hence every coin denomination is a multiple of it.

  • @likithareddy4627
    @likithareddy4627 7 років тому

    very nice work

  • @thesuperiorman8342
    @thesuperiorman8342 5 років тому

    This solution assumes the coins are arranged in a sorted array. If the coins were {2,1} your solution would fail. So you have to sort the array before applying the recursion.

  • @chosandesu
    @chosandesu 7 років тому +1

    I don't think this is correct. Try coins = [2, 5, 3, 6], amount = 10. Should be 5 but it gives 7 (has duplicates).

    • @vishnureddy3165
      @vishnureddy3165 6 років тому

      The above method only works if the coins are sorted. He didn't mention that

  • @govindarajukalyanaraman2579
    @govindarajukalyanaraman2579 7 років тому

    Excellent

  • @ankitsagar255
    @ankitsagar255 4 роки тому

    This will fail for test case :
    sum=4
    coins = {1 2 3}
    it should print 4, i.e
    {1,1,1,1}
    {1,1,2}
    {2,2}
    {1,3}
    your solution shows 7

  • @whatsmyname9742
    @whatsmyname9742 5 років тому

    i am too stupid!! can't formulate a recursion problem on my own. fml :(

    • @ONeillCode
      @ONeillCode  5 років тому +2

      Hey man, recursion is a tough concept to master and everyone struggles to become confident in creating recursion solutions. Just keep at it!

  • @deepakarora987
    @deepakarora987 6 років тому

    This is not the correct way of looking at this problem, you are introducing cycles in the problem, whereas Dynamic Programming has to be a topological sorting of a DAG ..

  • @shuyanli7366
    @shuyanli7366 6 років тому

    wrong answer.. the algorithm is wrong...
    i already tried in oj