"Microsoft 100" a step always jointly owned N-class, assuming that one can jump 1 levels, also can jump 2 levels, the total number of common total jump method, and analyze the time complexity of the algorithm

Source: Internet
Author: User

Package ms100;/** * A step will always have an n level, assuming you can jump 1 levels at a time. can also jump 2 levels, to find the total number of total common jump method. And analyze the time complexity of the algorithm * Note: This problem is often seen in the near future. including MicroStrategy and other more attention to the algorithm of the company has selected a problem as a face test or pen test.

First we consider the simplest case: if there are only 1 steps, there is obviously only one way to jump. Assuming there are 2 steps, there are two ways to jump: One is to jump two times. Jump 1 levels at a time, and the second one jumps 2 levels at a time. Now let's talk about the general situation: we think of the N-Step jumping method as a function of N. The memory is f (n).

When N>2. The first jump when there are two different choices: first, only jump 1, when the number of hops is equal to the rest of the n-1 steps of the number of jumps, that is f (n-1), the second option is the first jump 2, at this time the number of hops equal to the remaining n-2 steps of the number of hops, that is, F (n-2).

So the total number of different hops for n-Steps is f (n) = f (n-1) + f (n-2).

We summarize the above analysis with a formula such as the following: / 1 (n=1) f (n) = 2 (n=2) \ F (n-1) + (f-2) (n>2) */public Class Ms_27 {private int jumpstep (int n) {if (n<0) return 0;if (n==1| | n==2) return N;return jumpstep (n-1) + jumpstep (n-2);} public static void Main (string[] args) {ms_27 ms27 = new ms_27 (); System.out.println (Ms27.jumpstep (2));}}


"Microsoft 100" a step always jointly owned N-class, assuming that one can jump 1 levels, also can jump 2 levels, the total number of common total jump method, and analyze the time complexity of the algorithm

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.