Skip to content

Commit

Permalink
Create coinChange2.py
Browse files Browse the repository at this point in the history
  • Loading branch information
anaghayn authored Sep 8, 2023
1 parent fff843d commit 995b669
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions coinChange2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Time Complexity : O(m*n)
# Space Complexity : O(m*n)

class Solution(object):
def change(self, amount, coins):
"""
:type amount: int
:type coins: List[int]
:rtype: int
"""
row = len(coins) + 1
col = amount +1

dp = [[0 for x in range(col)] for x in range(row)]
dp[0][0] = 1

for i in range (1, row):
dp[i][0] = 1

for i in range (1, col):
dp[0][i] = 0

for i in range(1,row):
for j in range(1,col):
if coins[i-1]>j:
dp[i][j]=dp[i-1][j]
else:
dp[i][j]=dp[i-1][j]+dp[i][j-coins[i-1]]
return dp[row-1][col-1]

0 comments on commit 995b669

Please sign in to comment.