FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python/dynamic_programming/coin_change.py at master · singlav/Python · GitHub
singlav
Python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Python
/
dynamic_programming
/
coin_change.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (34 loc) · 1.02 KB
Breadcrumbs
Python
/
dynamic_programming
/
coin_change.py
Copy path
File metadata and controls
43 lines (34 loc) · 1.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
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
):
"""
>>> dp_count([1, 2, 3], 3, 4)
4
>>> dp_count([1, 2, 3], 3, 7)
8
>>> dp_count([2, 5, 3, 6], 4, 10)
5
>>> dp_count([10], 1, 99)
0
>>> dp_count([4, 5, 6], 3, 0)
1
"""
# table[i] represents the number of ways to get to amount i
table
=
[
0
]
*
(
n
+
1
)
# 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
coin_val
in
S
:
for
j
in
range
(
coin_val
,
n
+
1
):
table
[
j
]
+=
table
[
j
-
coin_val
]
return
table
[
n
]
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
Back
|
FazBrowse Home
|
New Git URL