Created
May 1, 2019 00:41
-
-
Save sanderhelleso/7d90e8be4218eaacd54c1202ccb88013 to your computer and use it in GitHub Desktop.
Implementation of the Fibonacci sequence using the dynamic programming concepts memozation & tabulation
This file contains 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 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