Love your videos, they're very helpful and probably the best I've seen for leetcode problems
@janmichaelaustria6203 жыл бұрын
There's also a BFS solution to this problem, which I thought was super cool! We can say the minimum coins need would be the minimum path from root to the needed amount. Then its similar to good old BFS! if not amount: return 0 visited = [False]*(amount+1) visited[0] = True q = deque([(0,0)]) while q: curr_amount, coinsUsed = q.popleft() #take coin coinsUsed += 1 for c in coins: #incremtn amount by the count next_amount = curr_amount + c if next_amount == amount: return coinsUsed #if we can still keep going if next_amount < amount: #check we havent seen this amount yet if visited[next_amount] == False: visited[next_amount] = True q.append((next_amount,coinsUsed)) return -1
Above solution throws error unless else case is changed to integer class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [float('inf') for i in range(amount + 1)] dp[0] = 0 for a in range(len(dp)): for c in coins: if a-c>= 0: dp[a] = min(dp[a], dp[a-c]+1) return -1 if dp[-1] == float('inf') else int(dp[-1])
@edwardteach23 жыл бұрын
class Solution(object): def coinChange(self, coins, amount): """ :type coins: List[int] :type amount: int :rtype: int """ dp = [float('inf')] * (amount + 1) dp[0] = 0 for i in range(1, amount + 1): for c in coins: if i - c >= 0: dp[i] = min(dp[i], 1 + dp[i-c]) return dp[amount] if dp[amount] != float('inf') else -1
@arpitsaxena2393 жыл бұрын
you need to make this video again .. not able to understand how dp[i]=min(dp[i], dp[i-c]+1) is working? 🤔
@leoyou47902 жыл бұрын
dp always track the minimum combination of coin to make up to i. dp[i] means current minimum combination , and dp[i-c]+1 means the combination if I take current coin, so we need to update dp[i] each coin to find the minimum combination.
@balavikashkandukuri61392 жыл бұрын
@@leoyou4790 could u make it more clear
@aetherllama83982 жыл бұрын
Time complexity is the same but this solution always runs in the worst case time. Average run time can be much faster.
@AnilKumar-pp2yo3 жыл бұрын
I wish you spend just a Lil more time to explain the logic 😫 solution looks great though
@yourdadishere3 жыл бұрын
Delete this video and do it again. Why you posting drafts online, man? Did you even watch your own video?