Skip to content

Instantly share code, notes, and snippets.

@hello-alf
Created August 2, 2020 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hello-alf/1396b9f90b277e820169ddb5f4ec433f to your computer and use it in GitHub Desktop.
Save hello-alf/1396b9f90b277e820169ddb5f4ec433f to your computer and use it in GitHub Desktop.
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