Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created January 15, 2018 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbalbert/4132cb6243974f916f5dd8be7646c069 to your computer and use it in GitHub Desktop.
Save davidbalbert/4132cb6243974f916f5dd8be7646c069 to your computer and use it in GitHub Desktop.
function countPossibilities(n, m) {
if (n === -1 || m === -1) {
return 0;
} else if (n === 0 && m === 0) {
return 1;
}
return countPossibilities(n-1, m) + countPossibilities(n, m-1);
}
function memoize(f) {
let memoTable = {};
return function (...args) {
const key = JSON.stringify(args);
if (!memoTable[key]) {
memoTable[key] = f(...args);
}
return memoTable[key];
}
}
countPossibilities = memoize(countPossibilities);
function fac(n) {
let res = 1;
while (n > 0) {
res *= n;
n -= 1;
}
return res;
}
function combinations(n, k) {
return fac(n)/(fac(k) * fac(n-k));
}
function efficientCountPossibilities(n) {
return Math.round(combinations(n*2, n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment