|
1 | 1 | """
|
2 | 2 | Problem
|
3 |
| -Given a value n, if we want to make change for N cents, |
4 |
| -and we have infinite supply of each of |
5 |
| -coins = {S1, S2, .. , Sm} valued coins, how many ways |
6 |
| -can we make the change? |
7 |
| -The order of coins doesn't matter. |
8 |
| -For example, for n = 4 and coins = [1, 2, 3], there are |
9 |
| -four solutions: |
| 3 | +Given a value `value`, if we want to make change for `value` cents, and we have infinite |
| 4 | +supply of each of coins = {S1, S2, .. , Sm} valued `coins`, how many ways can we make the change? |
| 5 | +The order of `coins` doesn't matter. |
| 6 | +For example, for `value` = 4 and `coins` = [1, 2, 3], there are four solutions: |
10 | 7 | [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3].
|
11 | 8 | So output should be 4.
|
12 | 9 |
|
13 |
| -For n = 10 and coins = [2, 5, 3, 6], there are five solutions: |
| 10 | +For `value` = 10 and `coins` = [2, 5, 3, 6], there are five solutions: |
| 11 | +
|
14 | 12 | [2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5] and [5, 5].
|
15 | 13 | So the output should be 5.
|
16 | 14 |
|
17 |
| -Time complexity: O(n * m) where n is the value and m is the number of coins |
| 15 | +Time complexity: O(n * m) where n is the `value` and m is the number of `coins` |
18 | 16 | Space complexity: O(n)
|
19 | 17 | """
|
20 | 18 |
|
| 19 | +def count(coins, value): |
| 20 | + """ Find number of combination of `coins` that adds upp to `value` |
21 | 21 |
|
22 |
| -def count(coins, n): |
| 22 | + Keyword arguments: |
| 23 | + coins -- int[] |
| 24 | + value -- int |
| 25 | + """ |
23 | 26 | # initialize dp array and set base case as 1
|
24 |
| - dp = [1] + [0] * n |
| 27 | + dp_array = [1] + [0] * value |
25 | 28 |
|
26 | 29 | # fill dp in a bottom up manner
|
27 | 30 | for coin in coins:
|
28 |
| - for i in range(coin, n+1): |
29 |
| - dp[i] += dp[i-coin] |
| 31 | + for i in range(coin, value+1): |
| 32 | + dp_array[i] += dp_array[i-coin] |
30 | 33 |
|
31 |
| - return dp[n] |
| 34 | + return dp_array[value] |
0 commit comments