Programmer interview question 2: Step jumping (abnormal step jumping)

Source: Internet
Author: User

Question 1: a step has a total of N levels. If you can skip 1 level at a time, you can also skip 2 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.

Question 2: a step has a total of N levels. If you can skip level 1 at a time, you can also skip level 2... it can also jump to level n. In this case, how many methods does the frog have to jump to an n-level step?
Analysis: fib (n) is used to represent the number of hops on the N-step. The number of hops on the N-step is 1 (n-step hop), and The FIB (0) is set) = 1;
When n = 1, there is only one hop method, that is, level 1 HOP: fib (1) = 1;
When n = 2, there are two hop modes: first hop and second HOP: fib (2) = fib (1) + fib (0) = 2;
When n = 3, there are three hops. After the first hop, there is also the FIB (3-1) Middle hop method. After the first hop, there is also the FIB (3-2) Middle hop method in the back; after the first jump out of the third level, there is also the FIB (3-3) Middle hop method in the back.
FIB (3) = fib (2) + fib (1) + fib (0) = 4;
When N = N, there are n hop methods, after the first jump out of the first order, there is a fib (n-1) Middle hop method; after the first jump out of the second order, there is a fib (n-2) .......................... after jumping out of order n for the first time, there is also the FIB (n-n) Middle hop method.
FIB (n) = fib (n-1) + fib (n-2) + fib (n-3) + .......... + fib (n-n) = fib (0) + fib (1) + fib (2) + ....... + fib (n-1)
And because fib (n-1) = fib (0) + fib (1) + fib (2) +... + fib (n-2)
Two formula subtraction: fib (N)-fib (n-1) = fib (n-1) ===="
FIB (n) = 2 * fib (n-1) n> = 2
Recursive equations are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.