Skip to content

Cleanup PEP-8 , additional comments , using coin_val, instead of rang… #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions dynamic_programming/coin_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
https://www.hackerrank.com/challenges/coin-change/problem
"""
from __future__ import print_function


def dp_count(S, m, n):

# table[i] represents the number of ways to get to amount i
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

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

return table[n]


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