* *********************************** Please specify the source: bytes ******************************************
I. Introduction
This morning, take a casual look.
See the article-> how to remove the "fish and fish" programmer in the interview
See the following section:
The most popular problem with recruiting programmers, especially code, is the fizz-Buzz test. If a programmer cannot write a fizz-buzz in 10-15 minutes, then he may need more exercises and may not be prepared at all. Another way is to ask them to write the Fibonacci series (Fibonacci series) and ask them to optimize it. We all know that Fibonacci is very common, but you may be surprised to see that these programmers can hardly write these series on top, even in IDE.
Well, I am not familiar with fizz-Buzz testing...
So wiki took a look-> http://en.wikipedia.org/wiki/Fizz_buzz
Okay, it's a bid game. The multiples of 3 or 3 call fizz, the multiples of 5 or 5 call buzz, and the multiples of 3 and 5 call fizzbuzz.
The focus is on the optimization of the next Fibonacci,
I only know the recursive and recursive search methods. I searched the internet and found a lot of optimizations.
We can see the time complexity O (log (N) space complexity O (1) method.
Just want to learn
Ii. fizz-buzz
I don't think it is difficult,
This is what I wrote:
<span style="font-family:Comic Sans MS;font-size:14px;">// Fizz Buzz void FizzBuzz( int n ){ bool isFZ; for( int i = 1 ; i <= n ; ++i ) { isFZ=false; if( i % 3 == 0 ) {cout<<"Fizz";isFZ=true;} if( i % 5 == 0 ) {cout<<"Buzz";isFZ=true;} if( !isFZ ) cout<<i; cout<<" "; if( i % 10 == 0 ) cout<<endl; }}</span>
Very simple, but I always think it's a bit complicated,
I hope the method will be better. Leave the code and learn it ~
Iii. Optimization of Fibonacci
Let's briefly describe the series of Fibonacci
There is an ideal creature, all of which are rabbit =.
At the beginning, there was such a rabbit. A rabbit was born at the beginning of each month, and a rabbit was born at the beginning of the third month.
In this case, how many rabbits will there be in the nth month?
To a table:
Month: 01 2 3 4 56... n
① Rabbit: 00 1 1 2 35... n-1 _ adult rabbit + n-1 _ ② rabbit
Derivation: n-1 _ adult rabbit = n-2 _ adult rabbit + N-2 _ ② rabbit, n-1 _ ② rabbit = n-2 _ ① rabbit
N-1-1 _ adult rabbit + n-1-1 _ ② rabbit = n-2 _ rabbit total
② Rabbit: 00 0 1 1 23... n-1 _ ① rabbit
Adult Rabbit: 01 1 1 2 35... n-1 _ adult rabbit + n-1 _ ② rabbit
Rabbit Total: 01 2 3 5 813... n-1 _ rabbit total + N-2 rabbit total
PS: ① rabbit indicates a rabbit of one month, and ② rabbit indicates a rabbit of two months. The number is the logarithm of the rabbit.
This is pushed down.
The numbers of ① rabbits in the nth month are equal to the numbers of adult rabbits in the current month.
The rabbit logarithm of the nth month is equal to the rabbit logarithm of the nth month.
The logarithm of the adult rabbit in the nth month is equal to the logarithm of the adult rabbit in the nth month + the logarithm of the rabbit in the second month.
Then the discovery of n-2 is pushed according to n-1.
The logarithm of rabbits in the nth month is equal to the sum of the logarithm of rabbits in the nth month and in the nth month.
This is based on my understanding .. It's a bit difficult to understand...
The basic concepts of Fibonacci have been completed. Now let's look at their solutions:
1. The simplest and most violent method to read: Recursive Method
Time Complexity: O (N ^ 2)
Space complexity: excessive numbers may cause stack overflow.
<span style="font-family:Comic Sans MS;font-size:14px;">int digui( int n ){ if( n == 0 ) return 0; else if( n == 1 ) return 1; else return ( digui(n-1) + digui(n-2) );}</span>
2. Saving space and time. Recursive advanced-Recursive Method
Time Complexity: O (N)
Space complexity: O (N)
<Span style = "font-family: Comic Sans MS; font-size: 14px;"> // General recursive algorithm int * ditui (int n) {int * arr = new int [n + 1]; arr [0] = 0, arr [1] = 1; for (INT I = 2; I <= N; + + I) Arr [I] = arr [I-1] + arr [I-2]; return arr ;}</span>
3. Continue Optimization-optimize the push Method
We can see that, if the number of the N ononacci is calculated,
We only need to know the number of Fibonacci for N-1 and N-2,
Storage is not required.
So there is more optimization,
Time Complexity: O (N)
Space complexity: O (1)
<Span style = "font-family: Comic Sans MS; font-size: 14px;"> // Recursive Algorithm Optimized int ditui_opt (int n) {If (n <2) return N; int I = 1, pre1 = 0, pre2 = 1; while (I <n) {pre2 = pre2 + pre1; pre1 = pre2-pre1; ++ I ;} return pre2 ;}</span>
4. More optimized-Matrix Method
Time Complexity: O (log (n ))
Space complexity: excessive numbers may cause stack overflow.
Recursion and Recursion cannot optimize the time complexity,
The space has reached O (1), and it cannot be refined,
So, is there any other way to further optimize it?
Of course!
We can find that F (n) is actually related to F (0) and F (1:
F (2) = F (1) + f (0 );
F (3) = F (2) + F (1) = 2 * F (1) + f (0 );
F (4) = f (3) + F (2) = 3 * F (1) + 2 * F (0 );
.......
Therefore, F (n) in the future must be equal:
F (n) = A * F (1) + B * F (0 );
But how can we find a and B?
The performance can be promoted through the matrix determinant:
In this way, the key is to calculate the power of the matrix,
If it is directly calculated, the time complexity is O (n), and there is no optimization at all.
Therefore, at this time, we need to use the bipartite method (divide and conquer) to solve the problem.
M ^ A = m ^ (A/2) * m ^ (A/2) = ....
In this way, the time complexity can be optimized to O (log (N ))!
<Span style = "font-family: Comic Sans MS; font-size: 14px;"> // matrix optimization method // construct a matrix struct matrix {int M0, M1, m2, M3;}; // matrix multiplication matrix mat_mul (matrix mtx1, matrix mtx2) {matrix MAT; mat. m0 = mtx1.m0 * mtx2.m0 + mtx1.m1 * mtx2.m2; mat. m1 = mtx1.m0 * mtx2.m1 + mtx1.m1 * mtx2.m3; mat. m2 = mtx1.m2 * mtx2.m0 + mtx1.m3 * mtx2.m2; mat. m3 = mtx1.m2 * mtx2.m1 + mtx1.m3 * mtx2.m3; return MAT;} // matrix multiplier matrix mat_pow (int K) {matrix MAT; If (k = 1) {mat. m0 = 1; mat. m1 = 1; mat. m2 = 1; mat. m3 = 0;} else if (K % 2 = 0) {MAT = mat_pow (K/2); MAT = mat_mul (MAT, mat );} else {MAT = mat_pow (k-1)/2); MAT = mat_mul (MAT, mat); MAT = mat_mul (MAT, mat_pow (1 ));} return MAT;} // finally obtain fibonacciint fib_matrix (int n) {If (n <2) return N; matrix MAT; MAT = mat_pow (n-1); int ans; ans = mat. m0 + mat. m1; return ans ;}</span>
This method is compressed to log (n) in time, but in space, because of the binary method, it is a recursive process,
Stack Overflow may occur.
So there is an optimization method.
5. Optimization
Time Complexity: O (log (n ))
Space complexity: O (1)
Yes, it's O (1 ).
Compression space complexity, and recursion,
That is to use the recursive to recursive method to derive,
I am still confused about this method and cannot tell it clearly,
During the transfer process,
Not all data needs to be stored,
So we will use the following in the beauty of programming:
.
The path to algorithms is profound and profound ..
* *********************************** Please specify the source: bytes ******************************************
Fizzbuzz and Fibonacci Optimization