Created
January 15, 2018 23:03
-
-
Save davidbalbert/4132cb6243974f916f5dd8be7646c069 to your computer and use it in GitHub Desktop.
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 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