From 72e3e3d380f6f9dec47bbd9732d3c2c42c90ff9d Mon Sep 17 00:00:00 2001 From: Aditya Bhuran Date: Thu, 11 Jun 2026 19:34:31 -0400 Subject: [PATCH] DP-1 --- problem1.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 problem1.py diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..516fec38 --- /dev/null +++ b/problem1.py @@ -0,0 +1,22 @@ +# Time Complexity : O(m*n) where m is the number of coins and n is the amount, because we are iterating through the dp array once +# Space Complexity : O(m*n) because we are using a 2D dp array of size m*n +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : yes, first time solving dp probem, had a hard time understanding the approach, but after watching the video and dry running the code, I was able to understand it and implement it successfully. + +class Solution(object): + def coinChange(self, coins, amount): + m, n = len(coins), amount + dp = [[0] * (n+1) for _ in range(m+1)] + + for j in range(1, n+1): + dp[0][j] = 99999 + + for i in range(1, m + 1): + for j in range(n + 1): + if j < coins[i - 1]: + dp[i][j] = dp[i - 1][j] + else: + dp[i][j] = min(dp[i - 1][j], 1 + dp[i][j - coins[i - 1]]) + + return -1 if dp[m][n] == 99999 else dp[m][n] + \ No newline at end of file