The Fibonacci sequence problem is a problem that the algorithm learner must be exposed to. As a classic problem, the first contact is usually a case tutorial as a recursive algorithm.
The recursion solves however Fibonacci. Its inefficiency was appalling, and it was calculated that its time complexity was O (2^n). The number of time complexity of the point.
Suppose the interviewer asks you about Fibonacci's solution, and you come up with a recursive solution that basically says you're already on the game.
So is there a more efficient algorithm? This article will introduce you.
Here are 4 solutions to Fibonacci:
1. Recursive time complexity O (2^n)
int f (int n) {if (n = = 1 | | n = = 2) {return 1;} return f (n-1) + f (n-2);}
2. Cycle time complexity O (n)
public int f (int n) { //write code here int F0 = 1; int f1 = 1; int F2 = 0; for (int i = 2; i < n; i++) { F2 = F0 + F1; F0 = F1; f1 = F2; } return f2; }
3. Matrix Solver time Complexity O (logn)
Fibonacci's recursive formula can be expressed in the form of matrices such as the following. So its
So according to the partition algorithm of matrix. The result can be calculated in O (logn) time.
Written questions:
for Fibonacci Classic questions. We are all familiar with it. By using the recursive formula f (n) = f (n-1) + f (n-2), we can find the nth item f (n)in linear time, Now consider the enhanced version of the Fibonacci retracement, we require that the range of items n is a nonnegative integer in the range of int, please design an efficient algorithm to calculate the nth Item F (n).
The first Fibonacci number is F (0) = 1.
given a non-negative integer, return to the Fibonacci sequence N item, in order to prevent overflow, use the result Mod 1000000007 .
Long[][] f = new long[][]{{0,1},{1,1}}; public int GetNthNumber1 (int n) { if (n = = 0) return 1; if (n = = 1) return 1; f = pow (n,f); return (int) f[1][1]; } Private long[][] POW (int n,long[][] f) { if (n = = 1) return F; if (n = = 2) { return fun (f,f) ; } if (n% 2 = = 0) {//even f = pow (n/2,f); Return fun (F, f); } else{ return Fun (Pow (n/2,f), pow (N/2 + 1,f)); } } Private long[][] Fun (long[][] f,long[][] m) { long[][] temp = new long[2][2]; Temp[0][0] = (F[0][0]*m[0][0] + f[0][1]*m[1][0])%1000000007; TEMP[0][1] = (f[0][0]*m[0][1] + f[0][1]*m[1][1])%1000000007; Temp[1][0] = (F[1][0]*m[0][0] + f[1][1]*m[1][0])%1000000007; TEMP[1][1] = (f[1][0]*m[0][1] + f[1][1]*m[1][1])%1000000007; return temp; }
4. Equation Solving time complexity O (1)
Yes, you read it right. The Fibonacci sequence is solved by a formula. Its general formula is as follows:
So, no matter what Fibonacci numbers can be calculated in O (1) time, but one thing, because of the irrational number involved. Therefore, the accuracy is not guaranteed.
Detail code slightly.
In summary, the most effective code efficiency is the most accurate, is the 3rd method of matrix solution, written interview must be mastered.
Finish
Fibonacci Efficient algorithm (4 algorithms for comprehensive analysis)