Skip to content

Instantly share code, notes, and snippets.

@hsnice16
Last active January 20, 2021 05:46
Show Gist options
  • Save hsnice16/f0615682c56319626fcfe2cb6e7e9839 to your computer and use it in GitHub Desktop.
Save hsnice16/f0615682c56319626fcfe2cb6e7e9839 to your computer and use it in GitHub Desktop.
/*
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