Last active
November 6, 2015 01:47
-
-
Save evansiroky/37a5c4e57a55fbc2a279 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
function coinSums(denominations, targetValue) { | |
var numCombos = 0, | |
cache = [], | |
combos = []; | |
var addFn = function(curValue, curCombo) { | |
for(var i=0; i < denominations.length; i++) { | |
var curDenom = denominations[i], | |
sum = curValue + curDenom, | |
copiedCombo = JSON.parse(JSON.stringify(curCombo)); | |
copiedCombo.push(curDenom); | |
var copiedComboKey = copiedCombo.sort().join(''); | |
if(cache.indexOf(copiedComboKey) == -1) { | |
cache.push(copiedComboKey); | |
if(sum == targetValue) { | |
if(combos.indexOf(copiedComboKey) == -1) { | |
combos.push(copiedComboKey); | |
numCombos += 1; | |
} | |
} else if(sum < targetValue) { | |
addFn(sum, copiedCombo); | |
} | |
} | |
} | |
} | |
addFn(0, []); | |
return numCombos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment