Last active
December 31, 2016 07:51
-
-
Save cyyeh/7bd87baa7c4da08117b11358ac331dc4 to your computer and use it in GitHub Desktop.
coin_change problem
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
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