(coins, amount, coin)
| 17 | }; |
| 18 | |
| 19 | var dfs = (coins, amount, coin) => { |
| 20 | let [max, minCost] = [amount / coins[coin], Infinity]; |
| 21 | |
| 22 | for (let num = 0; num <= max; num++) { |
| 23 | /* Time O(N) */ |
| 24 | const caUpdate = num * coins[coin] <= amount; |
| 25 | if (!caUpdate) continue; |
| 26 | |
| 27 | const product = num * coins[coin]; |
| 28 | const difference = amount - product; |
| 29 | const min = coinChange( |
| 30 | coins, |
| 31 | difference, |
| 32 | coin + 1, |
| 33 | ); /* Time O(S^N) | Space O(N) */ |
| 34 | const cost = min + num; |
| 35 | |
| 36 | const isSentinel = min === -1; |
| 37 | if (isSentinel) continue; |
| 38 | |
| 39 | minCost = Math.min(minCost, cost); |
| 40 | } |
| 41 | |
| 42 | return minCost !== Infinity ? minCost : -1; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * DP - Top Down |
no test coverage detected