forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#193 from erdenezul/coin_change_problem
add coin change problem
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 coins? | ||
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 |