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

Function coinChangeMin

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

Source from the content-addressed store, hash-verified

20 * @params {Number} amount
21 */
22export const coinChangeMin = (coins, amount) => {
23 const map = { 0: 1 }
24 for (let i = 1; i <= amount; i++) {
25 let min = Infinity
26 for (const coin of coins) {
27 if (i < coin) continue
28 min = Math.min(min, 1 + map[i - coin])
29 }
30 map[i] = min
31 }
32 return map[amount] === Infinity ? -1 : map[amount] - 1
33}

Callers 1

CoinChange.test.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected