MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / change

Function change

Dynamic-Programming/CoinChange.js:5–17  ·  view source on GitHub ↗
(coins, amount)

Source from the content-addressed store, hash-verified

3 * @params {Number} amount
4 */
5export 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

Callers 1

CoinChange.test.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected