Created
August 2, 2020 15:09
-
-
Save hello-alf/1396b9f90b277e820169ddb5f4ec433f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let res = [] | |
function greedyChange(coinSet, n, amount) { | |
if (n < 0) { | |
console.log( | |
'Lo sentimos no contamos con efectivo suficiente para darte el monto que necesitas' | |
) | |
return | |
} | |
if (amount === 0) { | |
return | |
} else { | |
if (amount - coinSet[n] >= 0) { | |
res.push(coinSet[n]) | |
return greedyChange(coinSet, n, amount - coinSet[n]) | |
} else { | |
return greedyChange(coinSet, n - 1, amount) | |
} | |
} | |
} | |
let coinSet = [1, 5, 10, 20] | |
greedyChange(coinSet, coinSet.length - 1, 21) | |
console.log('Cambio') | |
console.log(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment