-
Notifications
You must be signed in to change notification settings - Fork 118
/
count-the-coins.js
45 lines (33 loc) · 1.16 KB
/
count-the-coins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// There are four types of common coins in US currency: quarters (25 cents),
// dimes (10 cents), nickels (5 cents), and pennies (1 cent).
// Implement a function to determine how many ways there are to make change
// for a dollar using these common coins? (1 dollar = 100 cents).
function countCoinsTD () {
const coins = [1, 5, 10, 25];
const amount = 100;
const index = coins.length - 1;
const memo = {};
function countVariations(amount, index) {
if (amount < 0 || index < 0) return 0;
if (amount === 0) return 1;
const key = `${index}/${amount}`;
if (memo[key]) return memo[key];
memo[key] = countVariations(amount - coins[index], index) + countVariations(amount, index - 1);
return memo[key];
}
return countVariations(amount, index);
}
console.log(countCoinsTD()); // 242
function countCoinsBU () {
const coins = [1, 5, 10, 25];
const amount = 100;
const table = Array.from({ length: amount + 1 }).fill(0);
table[0] = 1;
for (let i = 0; i < coins.length; i++) {
for (let j = coins[i]; j <= amount; j++) {
table[j] += table[j - coins[i]];
}
}
return table[amount];
}
console.log(countCoinsBU()); // 242