This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,19 @@
function countcoins(t, o) {
'use strict';
var targetsLength = t + 1;
var operandsLength = o.length;
t = [1];
for (var a = 0; a < operandsLength; a ++) {
for (var b = 1; b < targetsLength; b ++) {
// initialise undefined target
t[b] = t[b] ? t[b] : 0;
// accumulate target + operand ways
t[b] += (b < o[a]) ? 0 : t[b - o[a]];
}
}
return t[targetsLength - 1];
}

View file

@ -0,0 +1,2 @@
countcoins(100, [1,5,10,25]);
242

View file

@ -0,0 +1,25 @@
function countcoins(t, o) {
'use strict';
var operandsLength = o.length;
var solutions = 0;
function permutate(a, x) {
// base case
if (a === t) {
solutions ++;
}
// recursive case
else if (a < t) {
for (var i = 0; i < operandsLength; i ++) {
if (i >= x) {
permutate(o[i] + a, i);
}
}
}
}
permutate(0, 0);
return solutions;
}

View file

@ -0,0 +1,2 @@
countcoins(100, [1,5,10,25]);
242