Skip to content

Commit 6037aeb

Browse files
authored
Create CoinChange.py
1 parent 051da83 commit 6037aeb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

CoinChange.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Dynamic Programming Python implementation of Coin
2+
# Change problem
3+
def count(S, m, n):
4+
5+
# table[i] will be storing the number of solutions for
6+
# value i. We need n+1 rows as the table is constructed
7+
# in bottom up manner using the base case (n = 0)
8+
# Initialize all table values as 0
9+
table = [0 for k in range(n+1)]
10+
11+
# Base case (If given value is 0)
12+
table[0] = 1
13+
14+
# Pick all coins one by one and update the table[] values
15+
# after the index greater than or equal to the value of the
16+
# picked coin
17+
for i in range(0,m):
18+
for j in range(S[i],n+1):
19+
table[j] += table[j-S[i]]
20+
21+
return table[n]
22+
23+
# Driver program to test above function
24+
arr = [1, 2, 3]
25+
m = len(arr)
26+
n = 4
27+
x = count(arr, m, n)
28+
print (x)
29+

0 commit comments

Comments
 (0)