Http://zhedahht.blog.163.com/blog/static/25411174200731844235261/
Question: a step has a total of N levels. If you can skip one level at a time, you can also skip two levels. Calculate the total number of hops in total and analyze the time complexity of the algorithm.
Analysis: This question has frequently appeared recently. Companies that focus on algorithms, such as microstrategy, have chosen this question as an interview question or a pen question.
First, consider the simplest situation. If there is only one level, there is obviously only one method to skip. If there are two steps, there are two ways to skip: one is to skip level 1 each time, and the other is to skip level 2 at a time.
Now let's discuss the general situation. We regard the jump Method for n-level steps as a function of N and record it as F (n ). When N> 2, there are two different options for the first hop: one is the first hop with only one level. At this time, the number of hops equals to the number of hops for the next n-1 level step, that is F (n-1); another option is the first jump level 2, at this time the number of Jump method is equal to the number of the next step of the N-2 level, that is, F (n-2 ). Therefore, F (n) = f (n-1) + (F-2) of different hops at N-level steps ).
We will summarize the above analysis using a formula as follows:
/1 n = 1
F (n) = 2 n = 2
\ F (n-1) + (F-2) n> 2
From this analysis, I believe many people can see that this is the familiar sequence of fiber ACCI. For more information about how to calculate the n of this sequence, see the 16th questions in this interview series.