File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments