-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolution.js
44 lines (35 loc) · 1.24 KB
/
solution.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
const getCoins = change => {
const AVAILABLE_COINS = [1, 2, 5, 10, 20, 50];
if (change <= 0) return Array(AVAILABLE_COINS.length).fill(0);
let remainingChange = change;
const changeReturn = [];
AVAILABLE_COINS.reverse().forEach((coin, index) => {
changeReturn[index] = Math.floor(remainingChange / coin);
remainingChange %= coin;
});
return changeReturn.reverse();
};
const getCoinsMap = change => {
const AVAILABLE_COINS = [1, 2, 5, 10, 20, 50];
if (change <= 0) return Array(AVAILABLE_COINS.length).fill(0);
let remainingChange = change;
return AVAILABLE_COINS.reverse()
.map(coin => {
const coinAmount = Math.floor(remainingChange / coin);
remainingChange %= coin;
return coinAmount;
})
.reverse();
};
const getCoinsReduce = change => {
const AVAILABLE_COINS = [1, 2, 5, 10, 20, 50];
if (change <= 0) return Array(AVAILABLE_COINS.length).fill(0);
let remainingChange = change;
return AVAILABLE_COINS.reduceRight((changeReturn, coin, index) => {
const previousChangeReturn = [...changeReturn];
previousChangeReturn[index] = Math.floor(remainingChange / coin);
remainingChange %= coin;
return previousChangeReturn;
}, []);
};
export { getCoins, getCoinsMap, getCoinsReduce };