Created
November 18, 2013 05:18
-
-
Save animatedlew/7522911 to your computer and use it in GitHub Desktop.
Finding how many combinations of change can be produced based on a total amount and list of denominations. Note that the Underscore.js dependency is required for legibility.
This file contains hidden or 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(_) { | |
var money = 300; | |
var coins = [5, 10, 20, 50, 100, 200, 500]; | |
var countChange = function(money, coins) { | |
if (money == 0) return 1; | |
else if (_.isEmpty(coins) || money < 0) return 0; | |
else return countChange(money - _.head(coins), coins) + countChange(money, _.tail(coins)); | |
}; | |
// 1022 | |
console.log(countChange(money, coins)); | |
}).call(this, _); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment