Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Time Complexity --> O(m*n) where m is the amount and n is the number of coins
# Space Complexity --> O(m)
# Approach --> Still going over all the possibilities but optimizing the search by reusing the result for repeated subproblems. This is the Tabulation DP, a bottom up approach. The space complexity is optimized to use only a single array of length m+1 since the result at each node only depends on its curent value or the value before that position in array.
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
n = len(coins)
m = amount
dp = [0 for i in range(m+1)]
for i in range(1,m+1):
dp[i] = amount+1

for i in range(1,n+1):
for j in range(1,m+1):
if j>=coins[i-1]:
dp[j] = min(dp[j], 1+dp[j-coins[i-1]])
#print(dp)
if dp[m]>amount:
return -1
return dp[m]