Skip to content

Commit aef8a84

Browse files
authored
Merge pull request prabhupant#177 from Uday032/Coin-change
Coin Change
2 parents 49d794f + 567c196 commit aef8a84

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Dynamic Programming Python implementation of Coin
2+
# Change problem
3+
def count(S, m, n):
4+
# We need n+1 rows as the table is constructed
5+
# in bottom up manner using the base case 0 value
6+
# case (n = 0)
7+
table = [[0 for x in range(m)] for x in range(n+1)]
8+
9+
# Fill the entries for 0 value case (n = 0)
10+
for i in range(m):
11+
table[0][i] = 1
12+
13+
# Fill rest of the table entries in bottom up manner
14+
for i in range(1, n+1):
15+
for j in range(m):
16+
17+
# Count of solutions including S[j]
18+
x = table[i - S[j]][j] if i-S[j] >= 0 else 0
19+
20+
# Count of solutions excluding S[j]
21+
y = table[i][j-1] if j >= 1 else 0
22+
23+
# total count
24+
table[i][j] = x + y
25+
26+
return table[n][m-1]

0 commit comments

Comments
 (0)