Skip to content

Instantly share code, notes, and snippets.

@Sashkan
Created January 4, 2023 16:41
Show Gist options
  • Save Sashkan/dcd9d4c536bbaa24111fb6a5fbf61607 to your computer and use it in GitHub Desktop.
Save Sashkan/dcd9d4c536bbaa24111fb6a5fbf61607 to your computer and use it in GitHub Desktop.
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