Skip to content

Instantly share code, notes, and snippets.

@evansiroky
Last active November 6, 2015 01:47
Show Gist options
  • Save evansiroky/37a5c4e57a55fbc2a279 to your computer and use it in GitHub Desktop.
Save evansiroky/37a5c4e57a55fbc2a279 to your computer and use it in GitHub Desktop.
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