forked from AllAlgorithms/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_change.py
More file actions
31 lines (22 loc) · 787 Bytes
/
Copy pathcoin_change.py
File metadata and controls
31 lines (22 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Dynamic Programming Python implementation of Coin Change
def count(S, m, n):
# case (n = 0)
table = [[0 for x in range(m)] for x in range(n+1)]
# Fill the entries for 0 value case (n = 0)
for i in range(m):
table[0][i] = 1
# Fill rest of the table entries in bottom up manner
for i in range(1, n+1):
for j in range(m):
# Count of solutions including S[j]
x = table[i - S[j]][j] if i-S[j] >= 0 else 0
# Count of solutions excluding S[j]
y = table[i][j-1] if j >= 1 else 0
# total count
table[i][j] = x + y
return table[n][m-1]
# Driver program to test above function
arr = [1, 2, 3]
m = len(arr)
n = 4
print(count(arr, m, n))