| 3 | * @params {Number} amount |
| 4 | */ |
| 5 | export const change = (coins, amount) => { |
| 6 | // Create and initialize the storage |
| 7 | const combinations = new Array(amount + 1).fill(0) |
| 8 | combinations[0] = 1 |
| 9 | // Determine the direction of smallest sub-problem |
| 10 | for (let i = 0; i < coins.length; i++) { |
| 11 | // Travel and fill the combinations array |
| 12 | for (let j = coins[i]; j < combinations.length; j++) { |
| 13 | combinations[j] += combinations[j - coins[i]] |
| 14 | } |
| 15 | } |
| 16 | return combinations[amount] |
| 17 | } |
| 18 | /** |
| 19 | * @params {Array} coins |
| 20 | * @params {Number} amount |