Nine-chapter algorithm surface test 60 climbing stairs

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/60/


Topics

There are n-level steps, and at first you stand on the No. 0 floor, each time you can climb two or one floor. Can you tell me how many different ways to climb to the nth level?

Follow up Question: If you can climb two levels at a time, and a reverse layer, the same position can not repeat, ask how many different ways to climb to the nth layer?


Online test

http://www.lintcode.com/problem/climbing-stairs/


Answer

This problem belongs to simple array one dimensional dynamic programming

1. State:f[i] Indicates the number of methods to crawl to layer I.

2. Function:f[i] = F[i-1] + f[i-2] The number of methods on the first layer is equal to the number of i-1 layers plus the number of i-2 layers

3. intialize:f[0] = 1, f[1] = 1 The number of methods that initially did not crawl and first layer was 1.

4. Answer:f[n] How many different ways to climb to the nth level


Follow up Question:

Compared with the original problem, this problem has raised a difficulty, mainly backward one layer, this place may violate the principle that the dynamic plan is not effective. So how are we going to transform it?

By condition: The same position cannot be repeated. We can know that if we want to regress, can not back two layers above, because two steps back two layers and then one step forward two layers, that will go the same position. So we can only get back one step at the most.

Then the conditions of the subject can be converted into two situations,

A. Jump two layers (forward two levels).

B. Step back to layer two (advance level).


1. State:f[i][0] indicates that the last step is the number of methods to crawl to layer I at level two. F[I][1] indicates that the last step is the number of methods to crawl to layer I in the case of jumping two layers.

2. function:f[i+1][1] = f[i][0] The last step is to skip a layer of jumping two layers of the case of the number of steps to climb to the i+1 layer is equal to the number of cases a from layer I to jump two layer back one layer. It is not possible to consider the method number of the case B in layer I, because the number of case B in layer I is taken back from layer i+1. F[i+2][0] = f[i][0]+f[i][1] The last step is to skip a layer of jumping two layers of the case to crawl to the i+2 layer of the number of methods equal to layer I all cases jump two layers.

3. The number of methods for intialize:f[0][0]=1 initialization that did not crawl at first was 1.4. Answer:f[n][0]+f[n][1] Climb to the nth layer A, B two different methods of the sum

Nine-chapter algorithm surface test 60 climbing stairs

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.