-
Notifications
You must be signed in to change notification settings - Fork 0
/
cashier.js
120 lines (96 loc) · 3.34 KB
/
cashier.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Imagine you landed a new job as a cashier...
Your quirky boss found out that you're a programmer and has a weird request about something they've been wondering for a long time.
Write a function that, given:
an amount of money
an array of coin denominations
computes the number of ways to make amount of money with coins of the available denominations.
Example: for amount=4 (4¢) and denominations=[1,2,3] (1¢, 2¢ and 3¢), your program would output 4—the number of ways to make 44¢ with those denominations:
1¢, 1¢, 1¢, 1¢
1¢, 1¢, 2¢
1¢, 3¢
2¢, 2¢
Gotchas
What if there's no way to make the amount with the denominations? Does your function have reasonable behavior?
*/
function getPossibilitiesForRecursive(amountLeft, denominations, currentIndex) {
currentIndex = currentIndex || 0;
if (amountLeft === 0) {
return 1;
}
if (amountLeft < 0 || currentIndex === denominations.length) {
return 0;
}
var numPossibilities = 0;
// choose a current coin
var currentCoin = denominations[currentIndex];
while (amountLeft >= 0) {
numPossibilities += getPossibilitiesForRecursive(amountLeft, denominations, currentIndex + 1);
amountLeft -= currentCoin;
}
return numPossibilities;
}
var cache = {};
function getPossibilitiesForAmountCache(amountLeft, denominations, currentIndex) {
currentIndex = currentIndex || 0;
if (cache[amountLeft + '-' + currentIndex]) {
return cache[amountLeft + '-' + currentIndex];
}
if (amountLeft === 0) {
//cache[amountLeft + '-' + currentIndex] = 1;
return 1;
}
if (amountLeft < 0 || currentIndex === denominations.length) {
//cache[amountLeft + '-' + currentIndex] = 0;
return 0;
}
var numPossibilities = 0;
// choose a current coin
var currentCoin = denominations[currentIndex];
while (amountLeft >= 0) {
numPossibilities += getPossibilitiesForAmountCache(amountLeft, denominations, currentIndex + 1);
amountLeft -= currentCoin;
}
cache[amountLeft + '-' + currentIndex] = numPossibilities;
return numPossibilities;
}
function getPossibilitiesForAmountBottomUp(amount, denominations) {
// intialize an array of zeros with indices up to amount
var waysOfDoingNcents = [];
for (var i = 0; i <= amount; i++) {
waysOfDoingNcents[i] = 0;
}
waysOfDoingNcents[0] = 1;
for (var j = 0; j < denominations.length; j++) {
var coin = denominations[j];
for (var higherAmount = coin; higherAmount <= amount; higherAmount++) {
var higherAmountRemainder = higherAmount - coin;
waysOfDoingNcents[higherAmount] += waysOfDoingNcents[higherAmountRemainder];
}
}
return waysOfDoingNcents[amount];
}
var denominations = [5, 6, 7, 8, 9, 10],
amount = 100;
console.time('recursive');
console.log({
amount,
denominations,
possibilities: getPossibilitiesForRecursive(amount, denominations)
});
console.timeEnd('recursive');
console.time('cache');
console.log({
amount,
denominations,
possibilities: getPossibilitiesForAmountCache(amount, denominations)
});
console.timeEnd('cache');
console.time('bottomup');
console.log({
amount,
denominations,
possibilities: getPossibilitiesForAmountBottomUp(amount, denominations)
});
console.timeEnd('bottomup');
process.exit();