-
Notifications
You must be signed in to change notification settings - Fork 0
/
change-making-brute-force.js
42 lines (33 loc) · 1.2 KB
/
change-making-brute-force.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
// Time Complexity (Brute Force): O(n^2)
const changeMakingGreedy = (availableCoins, amount) => {
let remainingAmount = amount
let coinCounter = {
selectedCoins: {},
numberOfCoins: 0
}
for ( const coin of availableCoins) {
const count = Math.floor(remainingAmount / coin)
coinCounter.selectedCoins[coin] = count
coinCounter.numberOfCoins += count
remainingAmount = remainingAmount - coin * count
}
return coinCounter
}
// BRUTE FORCE
const changeMakingBruteForce = (availableCoins, amount) => {
const results = []
for (var i = 0; i < availableCoins.length; i++ ) {
results.push(changeMakingGreedy(availableCoins.slice(i), amount)) // slice returns a shallow copy removing an index everytime
}
// Find the array with the smallest amount of coins
let smallestNumberOfCoins = Number.MAX_SAFE_INTEGER // largest int
let finalResult;
for (const result of results){
if (result.numberOfCoins < smallestNumberOfCoins) {
smallestNumberOfCoins = result.numberOfCoins
finalResult = result // if the smallestAmountOfCoins is founc, set the finalResult to the result
}
}
return finalResult
}
module.exports = { changeMakingBruteForce, changeMakingGreedy }