Programmer interview questions featured 100 questions (23)-step-jumping questions

Source: Internet
Author: User

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.

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.