I came across the following problem:
F(n)= 0 when n = 0;
F(n)= 1 when n = 1;
F(n)= F(n-1) + F(n-2) when n>1;
I can already solve this recursively like this:
int F(int n) {
if(n=0) return 0;
if(n=1) return 1;
if(n>1) return F(n-1) + F(n-2);
}
but the complexity is O(n^2)
. How can this be solved with the complexity O(n)
?
What book should I need to read to solve a problem like this?