Skip to content

Instantly share code, notes, and snippets.

@sanderhelleso
Created May 1, 2019 00:41
Show Gist options
  • Save sanderhelleso/7d90e8be4218eaacd54c1202ccb88013 to your computer and use it in GitHub Desktop.
Save sanderhelleso/7d90e8be4218eaacd54c1202ccb88013 to your computer and use it in GitHub Desktop.
Implementation of the Fibonacci sequence using the dynamic programming concepts memozation & tabulation
function fibMemo(n, memo) {
if (memo[n]) return memo[n];
if (n <= 1) return 1;
return memo[n] = fibMemo(n - 1, memo) + fibMemo(n - 2, memo);
}
function fibTab(n) {
const tab = new Array(n);
tab[0] = 1;
tab[1] = 1;
for (let i = 2; i <= n; i++) {
tab[i] = tab[i - 1] + tab[i - 2];
}
return tab[n];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment