-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-making-change.js
More file actions
40 lines (30 loc) · 876 Bytes
/
5-making-change.js
File metadata and controls
40 lines (30 loc) · 876 Bytes
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
denominationsFunc = (amount, denominations) => {
//the the smallest denomination
//make the amount
// convert small denominations from the first to bigger amounts.
denominations.sort((a,b) => a - b)
let stack = [];
let money = 0;
let ctr = 0;
//SMALLEST
while(money < amount) {
stack.push(denominations[0])
money = stack.reduce((acc, ele) => { return acc + ele}, 0)
}
if(money % amount === 0) ctr++
console.log(stack)
for(var i = 1; i < denominations.length; i++) {
let stackW = stack.slice(0)
let leftOver = Infinity;
while(leftOver >= denominations[i]) {
let popped = 0;
while(popped < denominations[i]) {
popped += stackW.pop();
}
ctr++
leftOver = stackW.reduce((acc, ele) => { return acc + ele}, 0)
}
}
return ctr
}
denominationsFunc(4, [1, 2, 3])