Coin Change - Dynamic Programming Bottom Up - Leetcode 322

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024

ความคิดเห็น • 283

  • @NeetCode
    @NeetCode  3 ปีที่แล้ว +38

    🚀 neetcode.io/ - A better way to prepare for Coding Interviews

    • @aaditkamat4995
      @aaditkamat4995 2 ปีที่แล้ว +1

      We can also initialize the array with amount instead of amount + 1 right, because the maximum number of coins used will be in the case where each coin you use is a 1 coin?

    • @jagdish-main
      @jagdish-main 2 ปีที่แล้ว +1

      @@aaditkamat4995 what if you have to form amount 10 and you have only 1 unit coin , than total number of coins will be 10 which is equal to the amount

  • @MP-ny3ep
    @MP-ny3ep ปีที่แล้ว +74

    Love how you went from the greedy approach to the brute force dfs approach to the optimal dp approach. It helped me understand this problem better. Thank you !

  • @MIDNightPT4
    @MIDNightPT4 2 ปีที่แล้ว +181

    Dynamic Programming is so confusing!!!!!!

    • @ehm-wg8pd
      @ehm-wg8pd 4 หลายเดือนก่อน +5

      yes its hard for me at begining, you should try with cases that has shaloe level of recursive and learn from there

    • @ivandrofly
      @ivandrofly 3 หลายเดือนก่อน +3

      YET beautiful :D

  • @richard12345789
    @richard12345789 3 ปีที่แล้ว +268

    actually neet's explanation to compare dfs,greedy and then using dp is soo rad and lowkey!! man u are awesome. give me a few more days i am gonna start donating for ur coffee's each week

    • @NeetCode
      @NeetCode  3 ปีที่แล้ว +35

      Thanks appreciate it a lot 😊

  • @adithyagowda4642
    @adithyagowda4642 3 ปีที่แล้ว +66

    The way you explained DFS approach and converted it into a DP solution was just freaking amazing!!! It's exactly what I was looking for. I now feel confident in converting my DFS solutions into DP solution. Thank you very much!

  • @DoJoStan
    @DoJoStan 3 ปีที่แล้ว +69

    Excellent explanation, I was trying to wrap my head around the bottom up approach solution described on leetcode and you illustrated it perfectly.

    • @lcgrind
      @lcgrind 29 วันที่ผ่านมา

      same! I was lost at what dp[i - coin[j]] meant, and it finally clicked when I watched this video at the @13:30s mark. Ty!

  • @cody_code
    @cody_code ปีที่แล้ว +51

    Wow, I was so stumped on this concept until I found this video. Thanks for going the extra mile with the explanation!

  • @wilsonwang8641
    @wilsonwang8641 2 ปีที่แล้ว +20

    The part at 12:20 really helps me to clarify the problem. Thanks bro Neetcode, you produce really meaningful content.

  • @PavelBogart
    @PavelBogart 2 ปีที่แล้ว +13

    love your videos mate
    whenever I am stuck I go straight to your channel instead of 'Solution' section.
    Keep it coming!

  • @souravchakraborty700
    @souravchakraborty700 2 ปีที่แล้ว +36

    Anyone curious about top to bottom recursion(backtracking with memoization) :
    def coinChange(self, coins: List[int], amount: int):
    memo = {}
    def dfs(amount):
    if amount == 0:
    return 0
    if amount

    • @vyshakr3058
      @vyshakr3058 ปีที่แล้ว +1

      thanks i was actually searching for this

  • @satyajeetkumarjha1482
    @satyajeetkumarjha1482 2 ปีที่แล้ว +6

    Your dp code is similar to what most of us will code but your code is more readable. Highly appreciable .

  • @pomegranate8593
    @pomegranate8593 ปีที่แล้ว +4

    excellent, I love how patient and carefully u explain instead of rushing stuff and skipping steps

  • @inquisitorshampoo1043
    @inquisitorshampoo1043 3 ปีที่แล้ว +21

    Have my amazon interview in 10 minutes. Your videos have been incredibly helpful while studying!

    • @chuongtruong7090
      @chuongtruong7090 3 ปีที่แล้ว +2

      How was your interview ?

    • @inquisitorshampoo1043
      @inquisitorshampoo1043 3 ปีที่แล้ว +58

      @@chuongtruong7090 good! ended up getting an offer

    • @chuongtruong7090
      @chuongtruong7090 3 ปีที่แล้ว +2

      @@inquisitorshampoo1043 Wow, congrats !!!!

    • @EDROCKSWOO
      @EDROCKSWOO 3 ปีที่แล้ว +3

      @@inquisitorshampoo1043 goddamn so lucky. I got rejected by google, a week ago. I cry.

    • @mastermind5421
      @mastermind5421 3 ปีที่แล้ว +2

      @@EDROCKSWOO what questions did they ask you?

  • @yipeng6239
    @yipeng6239 2 ปีที่แล้ว +12

    You are more qualified than most university instructors on introducing DSA topics, no joke

  • @wlcheng
    @wlcheng 2 ปีที่แล้ว +14

    Brilliant explanation! I was able to come up with the code by myself after understanding your solution. And the implementation is quite similar to yours. Guess I followed your coding style pretty well. 😆

  • @ahmadbasyouni9173
    @ahmadbasyouni9173 หลายเดือนก่อน

    your channel saves me so much time than trying to read the weird wording of the editorial on LC appreciate you man!

  • @madixit75
    @madixit75 2 ปีที่แล้ว +5

    Impressed, I was going nuts with leet code premium explanation. I owe you👌

  • @halahmilksheikh
    @halahmilksheikh 2 ปีที่แล้ว +10

    For anyone curious, here's what the top down DP with memoization looks like. It's very slow. If you remove the memoization, you get a time limit exceeded but it still "works"
    var coinChange = function(coins, amount) {
    let memo = [] // with memoization
    let ret = dfs(amount)
    if (ret == Infinity) ret = -1
    return ret
    function dfs(target) { // returns min from all paths
    if (target < 0) {
    return Infinity
    }
    if (target == 0) {
    return 0
    }
    if (memo[target] != null) {
    return memo[target]
    }
    memo[target] = Infinity
    for (let coin of coins) {
    memo[target] = Math.min(memo[target], 1 + dfs(target - coin))
    }
    return memo[target]
    }
    };

    • @mastermax7777
      @mastermax7777 ปีที่แล้ว +1

      why is it slow? I wrote it in python and it was just as fast as the dp solution he gave us...

    • @PippyPappyPatterson
      @PippyPappyPatterson 11 หลายเดือนก่อน

      @@mastermax7777 Agreed, shouldn't it be the same complexity?
      It only checks the number of paths to `0` once for each `range(amount)`.

  • @evyats9127
    @evyats9127 ปีที่แล้ว +1

    Nice and short functional programming style solution:
    dp = [0]
    for i in range(1, amount+1):
    options = [dp[i-x] for x in coins if i >= x and dp[i-x] != -1]
    dp.append(-1 if not options else 1+min(options))
    return dp[amount]

    • @kennyelkhart
      @kennyelkhart 11 หลายเดือนก่อน +1

      What is ‘functional’ about this? Also, why constantly resize an array if you know its size (amount+1) ahead to time?

  • @rjtwo5434
    @rjtwo5434 2 ปีที่แล้ว +7

    I was so close here but this helped to sort it out for me. Hands down the best explanations on this stuff. Thank you so much for this!

  • @linli7049
    @linli7049 3 ปีที่แล้ว +3

    When I search for a solution for a problem on leetcode, I will watch your video first. Excellent explanation!

  • @KevinN44
    @KevinN44 3 ปีที่แล้ว +5

    Awesome explanation! I was stuck with the top down approach and how to convert it to the DP solution, and this made it all click.
    Thanks!

  • @airman122469
    @airman122469 หลายเดือนก่อน

    There’s also a simple recursive way of doing this problem.
    Take the amount of change you’re looking for, find the largest denomination that does not go over the amount of change being requested, subtract that denomination from the amount requested, and call the same function with the new value.
    To account for values that are higher than the largest denomination, simply take the change amount value and subtract the integer division value that results from the change requested divided by the largest denomination, and again, call the function recursively.

  • @The6thProgrammer
    @The6thProgrammer 9 หลายเดือนก่อน

    If you are struggling to understand why we updated dp[i] when (i - coins[j] >= 0) the key insight is that dp[i] only gets updated if there is a solution that leads to zero (even when this condition evaluates to true). That is, there are cases where (i - coins[j] >= 0) but dp[i] remains "amount + 1". For instance if we have amount = 11 and coins = [2, 4, 6], when we get to i = 3, we find that 3 - 2 >= 0. However, min(dp[3], 1 + dp[3 - 2]) causes dp[3] to remain "amount + 1" as there is no valid solution leading to zero that we captured at dp[1] (that is, dp[1] itself still equals "amount + 1").

  • @keystarr
    @keystarr 28 วันที่ผ่านมา

    thank you, man, couldn't solve on my own and didn't understand both the Editorial and one of the top submissions. Only your explanation did it for me!

  • @emachine003
    @emachine003 หลายเดือนก่อน

    Thanks for the help... I figured out top-down memoization on my own, so fortunately most of my work carried over to the bottom-up approach.

  • @qualityhumour1510
    @qualityhumour1510 2 ปีที่แล้ว +6

    Your explanations skills are really amazing! Definitely very helpful! 🤓

  • @kwaminaessuahmensah8920
    @kwaminaessuahmensah8920 2 ปีที่แล้ว +5

    Bro, your explanations are so good. Keep up the amazing work!

  • @woostanley6290
    @woostanley6290 3 หลายเดือนก่อน

    This is such a hard question and I am really thankful that you are here to help to explain how to proceed with dp problems...

  • @juliahuanlingtong6757
    @juliahuanlingtong6757 3 ปีที่แล้ว +6

    Good explantion starting from recursion tree. However, during the transformation from recursion to bottom up dp, I really tried to search the answer to my 2 confusion, yet failed to find it.
    1. How did dp resolve the problem of dupliactes, eg, 1,5,1 and 1,1,5 are essentially the same and if we do through recursion, we have to add logic ro avoid that, but why dp arrays don't have to?
    2. How does it evolevs into a knapsack (take or not take ) problem? In another word, why dp[a]=min(dp[a], dp[a-c])?
    Looking forward to your reply to rsolve my huge confusions. Thanks!

    • @sumitevs
      @sumitevs 2 ปีที่แล้ว +2

      i have the same doubt. why dp[a]=min(dp[a], dp[a-c])?

    • @JasonKim1
      @JasonKim1 2 ปีที่แล้ว +1

      Duplicates don't matter here because what is cached is the minimum coins used. With your example, DP[7] is 3 whichever coin options are used.
      If the question has asked, return all the possible combinations of coins that make up the minimum number of coins uses, and the question explicitly asks you to dedupe cases like 1,1,5 and 1,5,1, then you have to care about dupes.

    • @RohithMusic
      @RohithMusic ปีที่แล้ว +1

      @@sumitevs I think we can think of it in a different way. dp[a] = 1+min(dp[a-c1],dp[a-c2], ...dp[a-ci]) where 1 to i is the different denominations of coins given. It looks confusing in code because he is doing a loop over ci and storing the min in dp[a] itself after every iteration.

  • @George-nx8zu
    @George-nx8zu ปีที่แล้ว +1

    Dynamic Programming is always so fuzzy to me. Thank you for explaining!

  • @jagdish-main
    @jagdish-main 2 ปีที่แล้ว +1

    I watched this problem in so many videos but all of them was forcing me to remember things and making this problem complex but you made it so intuitive like it's damm easy, thanks for helping :)

  • @alessandrocamillo4939
    @alessandrocamillo4939 3 ปีที่แล้ว +9

    Great video, great explanation. It starts from the brute force approach to build up he required intuition to eventually realize we can use a DP based solution. Thank you very much.
    One thing I wish to comment is that I am now curious to see a DP top-down recursive solution based on memoization. ;)

    • @namelesslamp12
      @namelesslamp12 3 ปีที่แล้ว

      Me too since i am new to dp memoization sometimes it's not clear to me :)

    • @GauravDhiman
      @GauravDhiman 2 ปีที่แล้ว +1

      Here is the python DP top-down recursive solution with memoization. It took me some time to come up with it.
      class Solution:
      def __init__(self):
      self.memo = {}
      self.coins = None

      def coinChange(self, coins: List[int], amount: int) -> int:
      if self.memo.get(amount) != None:
      return self.memo[amount]

      if amount == 0:
      return 0

      if self.coins == None:
      self.coins = sorted(coins)

      minc = float("inf")
      for c in self.coins:
      if amount < c:
      break
      res = self.coinChange(coins=None, amount=amount-c)
      if res == -1:
      continue
      minc = min(res + 1, minc)

      res = -1 if minc == float('inf') else minc
      self.memo[amount] = res
      return res

  • @spoorthi5230
    @spoorthi5230 2 ปีที่แล้ว +6

    Thank you! Definitely better explanation than the leetcode's explanation!

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +5

      Glad it was helpful!

  • @germanou
    @germanou 4 หลายเดือนก่อน

    Dude, your explanation ability is over the top. Amazing!

  • @andrewwong8483
    @andrewwong8483 7 หลายเดือนก่อน

    Really good explanation of the 1+dp[a] and what the 1 means in this case- because we’re using one coin as we’re iterating through the coins

  • @himeshsylesh
    @himeshsylesh ปีที่แล้ว

    You had made the best explanation found on youtube for this problem, with 3 different approaches. Thanks, buddy.

  • @SaurabhGupta-ei2hl
    @SaurabhGupta-ei2hl 2 ปีที่แล้ว +1

    Best Explanation on Dynamic Programming in a Scientific Approach!! 🙌🏻

  • @edenrosales8214
    @edenrosales8214 2 ปีที่แล้ว +3

    Are the dynamic programming and dfs solutions different in terms of space or time complexity?

  • @TheSRONIX
    @TheSRONIX 2 ปีที่แล้ว +1

    Thank you so much for the thought process of how to get to the dynamic programming solution!

  • @utberoxsobad
    @utberoxsobad 2 ปีที่แล้ว +1

    I got this question in an Amazon technical interview. I wish I saw this video beforehand

  • @Techgether
    @Techgether หลายเดือนก่อน

    Love the clear and concise explanation!

  • @georgpohl2665
    @georgpohl2665 2 ปีที่แล้ว +1

    Very well explained. Thank you i finally understood this problem. Keep at it!

  • @RobertPodosek
    @RobertPodosek 6 หลายเดือนก่อน

    This is brilliant. Really helped me understand dynamic programming. Thanks!

  • @ahmeeeeeeeeeeeed
    @ahmeeeeeeeeeeeed 3 ปีที่แล้ว +4

    Great video, deserves at least one million views in my opinion.

    • @NeetCode
      @NeetCode  3 ปีที่แล้ว

      Thanks, i appreciate it

  • @srinadhp
    @srinadhp 3 ปีที่แล้ว +1

    Thanks again! Your explanation helped a great deal in understanding these complex problems.

  • @nikhilaradhya4088
    @nikhilaradhya4088 หลายเดือนก่อน

    Good. You can start at min coin value in the outer for loo.
    It takes O(len(coins)) extra time to find the min. Helps in cases where amount is too large and number of coins of small.

  • @mrpotato2027
    @mrpotato2027 ปีที่แล้ว +1

    Extremely well explained. Thank you so much!

  • @ashleywilkonson386
    @ashleywilkonson386 3 ปีที่แล้ว +3

    Could you do the DP solution for Partition to K Equal Sum Subsets? There are literally no English explanations for it, only the backtracking approach.

  • @DanielVazquez
    @DanielVazquez 2 ปีที่แล้ว +2

    Greedy does work if we remove the biggest coin at every full iteration and keep checking for the minimum amount of change. This, however, is pretty inefficient.

  • @akhilraj6891
    @akhilraj6891 ปีที่แล้ว

    We can make the code work in constant space complexity by just storing the answer for last five amounts when calculate the current amount.

  • @ramonjales9941
    @ramonjales9941 2 ปีที่แล้ว

    i'm from Brazil. And you gained more one subscriber.Because your explanation is very good and you talk so cleary that i can undestand you. And this is so good for me that i'm learning english.

  • @n-julkushwaha2827
    @n-julkushwaha2827 ปีที่แล้ว

    one of the best solutions and explanation. Even though i knew the solution but still got stunned by your approach........

  • @hamzabendi9751
    @hamzabendi9751 2 ปีที่แล้ว +2

    I don't think this solution would work in all cases.
    Let's say you can only use the coins (2, 5, 10) to return a value of 2, at the first iteration and when trying to calculate dp[1] you're gonna find out that no coin is smaller that 1 so the condition "a - c >= 0" will not be met, and so dp[1] will stay equal to amount+1 (and this is gonna screw what comes next). So if you try to know the number of coins needed to return a value of 2 (it should be 1 cause we have 2 in our list of coins), this algorithm will return -1 because you will have dp[2] = amount+1.
    I'm not sure if i explained my process of thoughts right, if you think i'm wrong i'd be happy to learn.

    • @mk-19memelauncher65
      @mk-19memelauncher65 2 ปีที่แล้ว +1

      Dp2 will be min of maxInt and 1+ dp[2-2] which hits the basecase of 0 and returns 1 correctly

  • @psychogalvanometer8945
    @psychogalvanometer8945 ปีที่แล้ว +1

    Finally found a solution that I can understand. Thank you Sir!

  • @hudsonriverrunner
    @hudsonriverrunner 3 ปีที่แล้ว +1

    for those who wanna know the optimal, check bidirectional BFS with DP, it beats 99.6%

  • @siddharthsharma3679
    @siddharthsharma3679 3 ปีที่แล้ว

    Dude !!!
    I don't usually drop comments but this was amazing !
    Crisp explanation, thank you soo much.

  • @Ryan-sd6os
    @Ryan-sd6os ปีที่แล้ว +1

    you dont really explain the dp formula from dp[2] to the end

  • @CasualyinAir
    @CasualyinAir 3 ปีที่แล้ว +1

    Thank you. Your explanation is really clear.

  • @johnhammer8668
    @johnhammer8668 2 ปีที่แล้ว

    Thankyou very much. The DFS part is just amazing. It was visually intuitive.

  • @Babe_Chinwendum
    @Babe_Chinwendum 2 ปีที่แล้ว

    Mann, my first approach was greedy. Thanks for explaining why it did not work out.

  • @koubbe
    @koubbe 2 ปีที่แล้ว

    I know this problem is best solved by using Dynamic Programming, but I want to focus on the Brute Force solution:
    I can say it is Divide and Conquer: We are dividing the problem into smaller sub-problems, solving individually and using those individual results to construct the result for the original problem.
    I can also say it is Backtracking: we are enumerating all combinations of coin frequencies which satisfy the constraints.
    I know both are implemented via Recursion, but I would like to know which paradigm the Brute Force solution belongs to: Divide and Conquer or Backtracking.

  • @halahmilksheikh
    @halahmilksheikh 2 ปีที่แล้ว

    You literally explained this problem better than my algorithms professor at a UC

  • @jackchan6266
    @jackchan6266 ปีที่แล้ว

    i commented on the code for my learning hopefully this is helpful
    class Solution(object):
    def coinChange(self, coins, amount):
    """
    :type coins: List[int]
    :type amount: int
    :rtype: int
    """
    dp = [amount + 1] * (amount + 1) # 0 to amount so amount + 1 in total
    dp[0] = 0 # base case, amount 0 takes 0 coins
    print(dp)
    #bottom up order
    for a in range(1, amount + 1): # to get to each number in 1 to the amount + 1
    for c in coins: # go through every coin
    if a - c >= 0: # remainder
    dp[a] = min(dp[a], 1 + dp[a - c]) # go through every possible solution
    # coin = 4, a = 7, dp[7] = 1 + dp[7-4] = 1 + dp[3]
    # dp stores the number of coins needed to get to that number, therefore we get 1 for in 1 + dp[a - c] for an additional coin needed

    print(dp)
    return dp[amount] if dp[amount] != amount + 1 else -1
    # dp[amount] != amount + 1 is the default value

  • @georgechen1124
    @georgechen1124 3 หลายเดือนก่อน

    Well explained, bro! Impressive DP solution!

  • @RanjuRao
    @RanjuRao 3 ปีที่แล้ว +1

    crystal clear illustration :) awesome ! Thanks for the video

  • @SelftaughtSoftwareEngineer
    @SelftaughtSoftwareEngineer 9 วันที่ผ่านมา

    What software do you use to illustrate the example?

  • @AfterThisShutUp
    @AfterThisShutUp หลายเดือนก่อน

    This can also be solved using breadth-first search since we are trying to find the shortest path from amount to 0.

  • @Nerdimo
    @Nerdimo 2 ปีที่แล้ว

    Definitely a confusing one, I hope sometime in the future I understand it better. Thanks for the tip

  • @sekirandahamza1260
    @sekirandahamza1260 ปีที่แล้ว

    Very well explained. I'm a newbie to dp in general, Do people solve these problems off head in interviews? or they just cram the solutions and go for it, because I'm having trouble wrapping my head around these problems.

  • @bikcrum
    @bikcrum 2 ปีที่แล้ว

    I was able to code myself after looking at your video. Great explanation.

  • @user-xz8oj8ji8i
    @user-xz8oj8ji8i ปีที่แล้ว

    Really clear pronunciation! Also the explanations!

  • @joq6356
    @joq6356 2 ปีที่แล้ว

    Thanks for the explanation, this is great!
    This might be a silly question but I didn’t understand the return part quite well, does it mean that if the amount cannot be summed by the giving coins then return -1? But the only case was amount 0 which was listed out at the beginning. Or is this a general sentence to cover any case not just the example in description? Thanks!

  • @gn03398604
    @gn03398604 ปีที่แล้ว

    It's a pretty clear solution. really helps me a lot! appreciate!!!

  • @enisarik6002
    @enisarik6002 2 ปีที่แล้ว

    First understand DFS very well, and then try to understand bottom-up DP concept. Coding is trivial, but understanding the whole solution is really hard.

  • @muhammetguduk2020
    @muhammetguduk2020 2 ปีที่แล้ว

    Wish I saw this question earlier. I'd have nailed the Intel's coding interview ... Only one that I couldn't solve was this one..

  • @premrr8330
    @premrr8330 3 ปีที่แล้ว

    Next level explanation! Instantly subscribed! Thank you

  • @superheroherohero
    @superheroherohero 2 ปีที่แล้ว

    Thank you, very very clear, your videos have very high quality.

  • @lucasshi1091
    @lucasshi1091 9 หลายเดือนก่อน

    Does time complexity differ if we choose top-down dynamic programming?
    I think top-down is better because we don't have to compute every number in the range of amount. Am I right?

  • @danielsun716
    @danielsun716 2 ปีที่แล้ว

    couple questions: 1. dp[a] = min(dp[a], 1 + dp[a - c]), why should compare itself with 1 + dp[a-c]? is it possible dp[a] it self less than 1 + dp[a- c]? 2. why the condition is if dp[amount] != amount + 1:? if coins = [2], amount = 3, how should it explain under this situation? I am so confused.

  • @ThEHaCkeR1529
    @ThEHaCkeR1529 2 ปีที่แล้ว +1

    Why does it remain amount+1 if it is impossible to create the combination?

  • @symbol767
    @symbol767 2 ปีที่แล้ว

    Sick, I tried this for the first time on my own and used BFS to find the "shortest path". Sadly I knew I needed to optimize it with caching but couldn't figure out the right way to implement it. When I saw I only needed 3 extra lines to finish my solution I felt hella dumb. But this is a really good problem.

  • @nikhilgoyal007
    @nikhilgoyal007 7 หลายเดือนก่อน

    self notes: Not greedy - since 5 + 1 + 1 will not work; backtracking - yea but overlapping subproblems so can use DP; Memoization works ; now think in reverse and do bottoms up .

    • @edreesomer9851
      @edreesomer9851 7 หลายเดือนก่อน

      What is the difference between back tracking and dfs? I know dft in graph but how is it used here?

  • @kobebyrant9483
    @kobebyrant9483 3 ปีที่แล้ว +1

    Great explanation. Crystal clear!

  • @Asmrnerd10
    @Asmrnerd10 11 หลายเดือนก่อน

    The correct solution on leetcode has ur OUTER loop as its INNER LOOP and your INNER loop as its OUTER loop. Why is it switched around? For example the solution on leetcode says this:
    Bottom up DP
    class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
    dp=[math.inf] * (amount+1)
    dp[0]=0
    for coin in coins:
    for i in range(coin, amount+1):
    if i-coin>=0:
    dp[i]=min(dp[i], dp[i-coin]+1)
    return -1 if dp[-1]==math.inf else dp[-1]

    • @kennyelkhart
      @kennyelkhart 11 หลายเดือนก่อน

      It’s just enumerating the coins first. The time complexity is the same and so is the result, as the correct minimum is still discovered.

  • @yinglll7411
    @yinglll7411 2 ปีที่แล้ว

    thank you so much! loving every single video! Great explanations!

  • @Chriscs7
    @Chriscs7 3 หลายเดือนก่อน

    What if you also want to store the coins used and not just return the minimum number of coins for a particular amount? Can you modify the code to do that

  • @bonle3771
    @bonle3771 9 หลายเดือนก่อน

    arent both DP? first one seems to be easier to think of. One of the other way I was doin. is to traverse the sorted coins from the

  • @habibhasanshakil9086
    @habibhasanshakil9086 3 ปีที่แล้ว

    Just awesome. subscribed your channel. huge love and respect for you bro.

  • @warnercooler4488
    @warnercooler4488 3 ปีที่แล้ว

    Awesome explanation! Thank you so much!

  • @samrat_malisetti
    @samrat_malisetti 4 หลายเดือนก่อน

    It's good to have the Pseudo code of brute force solution using recursion. Because I was confused about the base cases.

  • @abhilash4976
    @abhilash4976 2 ปีที่แล้ว +1

    can anyone explain, 1+ dp[6]. what if we dont have a coin of value 1 to make the value 7, because in the code it is 1+dp[a-c]

    • @sparrow2068
      @sparrow2068 2 ปีที่แล้ว

      the 1 doesn't represent 1 cent, it represents 1 coin

  • @vangelis_tr6132
    @vangelis_tr6132 2 หลายเดือนก่อน

    i dont understand the "if dp[amount] != amount + 1 else return -1". Why is this if statement valid?

  • @JimmyKal
    @JimmyKal 5 หลายเดือนก่อน

    1 minute of silence for the guys that went into brute force in an interview

  • @3rd_iimpact
    @3rd_iimpact ปีที่แล้ว

    At 17:19, why are we adding a 1? He explains but I'm still confused.

  • @omerrivlin2637
    @omerrivlin2637 ปีที่แล้ว

    Amazing explanation man! Thank you so much :)

  • @harishsn4866
    @harishsn4866 2 ปีที่แล้ว

    You are the best. Thank you so much for this explanation.

  • @Call-me-Avi
    @Call-me-Avi 2 ปีที่แล้ว

    Pretty neat explanation man. Thank you

  • @dorondavid4698
    @dorondavid4698 2 ปีที่แล้ว

    Question, why did you say for a in range(1, amount + 1) is bottom up when it counts from 1-8?

  • @pfiter6062
    @pfiter6062 10 หลายเดือนก่อน +1

    if we were given an input like this
    coins = [50], amount = 100
    would the dfs solution faster than dp solution?

    • @m____s
      @m____s 3 หลายเดือนก่อน

      that was my doubt too