NODE
node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const minChange = (amount, coins) => {
const answer = _minChange(amount, coins);
return answer === Infinity ? -1 : answer;
};
const _minChange = (amount, coins, memo = {}) => {
if (amount < 0) return Infinity;
if (amount === 0) return 0;
if (amount in memo) return memo[amount];
let min = Infinity;
for (let coin of coins) {
const numCoins = 1 + _minChange(amount - coin, coins, memo); // 1 is supoused to be the number of coins that were needed it looks weird to me
min = Math.min(numCoins, min);
}
return (memo[amount] = min); // why
};
console.log(minChange(13, [1, 9, 5, 14, 30]));
// the problem and code is from this course https://structy.net/problems/min-change
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run