Skip to content

Commit 18856a6

Browse files
authored
Create Coin Change 2.py
1 parent 651604d commit 18856a6

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Coin Change 2.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
3+
4+
Note: You can assume that
5+
6+
0 <= amount <= 5000
7+
1 <= coin <= 5000
8+
the number of coins is less than 500
9+
the answer is guaranteed to fit into signed 32-bit integer
10+
11+
12+
13+
Example 1:
14+
15+
Input: amount = 5, coins = [1, 2, 5]
16+
Output: 4
17+
Explanation: there are four ways to make up the amount:
18+
5=5
19+
5=2+2+1
20+
5=2+1+1+1
21+
5=1+1+1+1+1
22+
23+
24+
25+
Example 2:
26+
27+
Input: amount = 3, coins = [2]
28+
Output: 0
29+
Explanation: the amount of 3 cannot be made up just with coins of 2.
30+
31+
32+
33+
Example 3:
34+
35+
Input: amount = 10, coins = [10]
36+
Output: 1
37+
38+
'''
39+
40+
class Solution(object):
41+
def change(self, amount, coins):
42+
"""
43+
:type amount: int
44+
:type coins: List[int]
45+
:rtype: int
46+
"""
47+
f = [0 for i in xrange(amount + 1)]
48+
f[0] = 1
49+
50+
for c in coins:
51+
for i in range(c, len(f)):
52+
f[i] += f[i-c]
53+
54+
return f[-1]

0 commit comments

Comments
 (0)