Last active
January 20, 2021 05:46
-
-
Save hsnice16/f0615682c56319626fcfe2cb6e7e9839 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
/* | |
optimization of fibonacci numbers implementation | |
time comlexity : O(n) rather than exponential ,as in older recursive implementation. | |
*/ | |
int fib(int n, int memo[]) | |
{ | |
if (memo[n] == -1) // if we encounter this number first number, as memo[] is initialized with -1 | |
{ | |
int res; | |
if (n == 0 || n == 1) | |
{ | |
res = n; | |
} | |
else | |
{ | |
res = fib(n - 1, memo) + fib(n - 2, memo); | |
} | |
memo[n] = res; | |
} | |
return memo[n]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment