Skip to content

Commit c35e1e0

Browse files
authored
Create coin-change-2.py
1 parent b6475fb commit c35e1e0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Python/coin-change-2.py

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

0 commit comments

Comments
 (0)