From 96536ad7617dadb74b89a8ebc14cc239f8f3b294 Mon Sep 17 00:00:00 2001 From: Erdenezul Date: Wed, 25 Oct 2017 09:56:48 +0800 Subject: [PATCH 1/2] add coin change problem --- dynamic_programming/coin_change.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 dynamic_programming/coin_change.py diff --git a/dynamic_programming/coin_change.py b/dynamic_programming/coin_change.py new file mode 100644 index 000000000..a8af4d5cc --- /dev/null +++ b/dynamic_programming/coin_change.py @@ -0,0 +1,25 @@ +""" +You have m types of coins available in infinite quantities +where the value of each coins is given in the array S=[S0,... Sm-1] +Can you determine number of ways of making change for n units using +the given types of coints? +https://www.hackerrank.com/challenges/coin-change/problem +""" +def dp_count(S, m, n): + table = [0] * (n + 1) + + # Base case (If given value is 0) + 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]] + + 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 From 53ce9b806813ec253aaf6750c595d026fb84a434 Mon Sep 17 00:00:00 2001 From: Erdenezul Date: Wed, 25 Oct 2017 15:58:46 +0800 Subject: [PATCH 2/2] fix typo --- dynamic_programming/coin_change.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/coin_change.py b/dynamic_programming/coin_change.py index a8af4d5cc..dca016359 100644 --- a/dynamic_programming/coin_change.py +++ b/dynamic_programming/coin_change.py @@ -2,7 +2,7 @@ You have m types of coins available in infinite quantities where the value of each coins is given in the array S=[S0,... Sm-1] Can you determine number of ways of making change for n units using -the given types of coints? +the given types of coins? https://www.hackerrank.com/challenges/coin-change/problem """ def dp_count(S, m, n):