Created
January 4, 2023 16:41
-
-
Save Sashkan/dcd9d4c536bbaa24111fb6a5fbf61607 to your computer and use it in GitHub Desktop.
This file contains 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
const [amount] = process.argv.slice(2); | |
function getChange(given, denominations = [10, 5, 2]) { | |
if (!given || given === 0) { | |
return []; | |
} | |
let minChange = Infinity; | |
let minChangeArr; | |
for (const coin of denominations) { | |
if (coin > given) { | |
continue; | |
} | |
const change = getChange(given - coin, denominations); | |
if (change && change.length + 1 < minChange) { | |
minChange = change.length + 1; | |
minChangeArr = change.concat(coin); | |
} | |
} | |
return minChangeArr?.reverse(); | |
} | |
console.log(getChange(amount)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment