MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / dfs

Function dfs

javascript/0322-coin-change.js:19–43  ·  view source on GitHub ↗
(coins, amount, coin)

Source from the content-addressed store, hash-verified

17};
18
19var 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

Callers 1

coinChangeFunction · 0.70

Calls 1

coinChangeFunction · 0.70

Tested by

no test coverage detected