Skip to content

Commit 98a149e

Browse files
pradyotdpoyea
authored andcommitted
Update coin_change.py (TheAlgorithms#706)
Cleanup PEP-8 , additional comments , using coin_val, instead of range and index.
1 parent ad68eed commit 98a149e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

dynamic_programming/coin_change.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66
https://www.hackerrank.com/challenges/coin-change/problem
77
"""
88
from __future__ import print_function
9+
10+
911
def dp_count(S, m, n):
12+
13+
# table[i] represents the number of ways to get to amount i
1014
table = [0] * (n + 1)
1115

12-
# Base case (If given value is 0)
16+
# There is exactly 1 way to get to zero(You pick no coins).
1317
table[0] = 1
1418

1519
# Pick all coins one by one and update table[] values
1620
# after the index greater than or equal to the value of the
1721
# picked coin
18-
for i in range(0, m):
19-
for j in range(S[i], n + 1):
20-
table[j] += table[j - S[i]]
22+
for coin_val in S:
23+
for j in range(coin_val, n + 1):
24+
table[j] += table[j - coin_val]
2125

2226
return table[n]
2327

28+
2429
if __name__ == '__main__':
2530
print(dp_count([1, 2, 3], 3, 4)) # answer 4
2631
print(dp_count([2, 5, 3, 6], 4, 10)) # answer 5

0 commit comments

Comments
 (0)