Skip to content

Instantly share code, notes, and snippets.

@cyyeh
Last active December 31, 2016 07:51
Show Gist options
  • Save cyyeh/7bd87baa7c4da08117b11358ac331dc4 to your computer and use it in GitHub Desktop.
Save cyyeh/7bd87baa7c4da08117b11358ac331dc4 to your computer and use it in GitHub Desktop.
coin_change problem
function coin_change(coinType, coinToChange) {
if (coinToChange <= 0)
return 0;
var arr = [];
for (var i = 0; i < coinType.length; i++) {
if (coinToChange >= coinType[i])
arr.push(1 + coin_change(coinType, (coinToChange - coinType[i])));
}
return Math.min.apply(Math, arr);
}
var coinType = [33, 24, 12, 5, 1];
var coinToChange = 36;
coin_change(coinType, coinToChange);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment