I understand why DP is more efficient over than simple recursion.
However, I understood that DP is recursion using memoization technique.
For Fibonacci, like this :
int DP[10000]; //initalized by 0
int f(int num){
if (DP[num] != 0)
return DP[num];
DP[num] = f(num -1) + f(num - 2);
return DP[num];
}
I always use DP in this way of recursion, and it works quite well on algorithm problems like ACM.
Recently, I knew that most people do not use DP in this way.
They use DP like this :
int fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[n+1];
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
this source is from http://www.geeksforgeeks.org/program-for-nth-fibonacci-number/
I don't know this two way's difference.
Are they have same time complexity?
Also, I wonder can my way(recursion + memoization) is called DP?
And is there any disadvantages using my way of DP on algorithm problems?