mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Update coin_change.py (#706)
Cleanup PEP-8 , additional comments , using coin_val, instead of range and index.
This commit is contained in:
parent
ad68eed73e
commit
98a149e41e
@ -6,21 +6,26 @@ the given types of coins?
|
|||||||
https://www.hackerrank.com/challenges/coin-change/problem
|
https://www.hackerrank.com/challenges/coin-change/problem
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
|
||||||
def dp_count(S, m, n):
|
def dp_count(S, m, n):
|
||||||
|
|
||||||
|
# table[i] represents the number of ways to get to amount i
|
||||||
table = [0] * (n + 1)
|
table = [0] * (n + 1)
|
||||||
|
|
||||||
# Base case (If given value is 0)
|
# There is exactly 1 way to get to zero(You pick no coins).
|
||||||
table[0] = 1
|
table[0] = 1
|
||||||
|
|
||||||
# Pick all coins one by one and update table[] values
|
# Pick all coins one by one and update table[] values
|
||||||
# after the index greater than or equal to the value of the
|
# after the index greater than or equal to the value of the
|
||||||
# picked coin
|
# picked coin
|
||||||
for i in range(0, m):
|
for coin_val in S:
|
||||||
for j in range(S[i], n + 1):
|
for j in range(coin_val, n + 1):
|
||||||
table[j] += table[j - S[i]]
|
table[j] += table[j - coin_val]
|
||||||
|
|
||||||
return table[n]
|
return table[n]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(dp_count([1, 2, 3], 3, 4)) # answer 4
|
print(dp_count([1, 2, 3], 3, 4)) # answer 4
|
||||||
print(dp_count([2, 5, 3, 6], 4, 10)) # answer 5
|
print(dp_count([2, 5, 3, 6], 4, 10)) # answer 5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user