Leetcode notes: Climbing Stairs)

Source: Internet
Author: User

Leetcode notes: Climbing Stairs)

I. Description

You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

The general idea of the question is that there are known n-level stairs, each time you can only climb level 1 or Level 2 stairs, there are several ways to climb the n-level stairs-_-|. The question can be viewed as, Setf(n)Indicates crawling tonThe number of steps to climb to the n-level stair can be either of the following two options:
• Fromf(n-1)Progress1Step;
• Fromf(n-2)Progress2Step;
Thenf(n)Can be written:f(n) = f(n-1) + f(n-2)

The question is transformed into the Fibonacci series. There are many online studies on this topic:
Http://baike.baidu.com/link? Url = c2Bmk2jBGbI46qTIA-qKmdTkYBrVYYrejAHzf8BJRwCekIL4Sbx48fFCRkeGdul0

Ii. Question Analysis

For the Fibonacci sequence, recursion or iteration can be used to solve the problem. The column in this book can be written as the following recursive relationship:

Obviously, iterative iteration using recursive relational expressions is not the optimal solution. When calculating the f (n) value, we need to calculate f (1), f (2 ),..., All values of f (n-1) exist in many repeated operations, such as f (4) = f (3) + f (2), where f (3) needs to be solved) = f (2) + f (1 ). If an array is used to store all computed items, the time complexity can be reduced to O (n), and the space complexity is O (n ).

In order to pursue time complexity, we use the Fibonacci generic formula directly. The derivation process of this formula is as follows:

Iii. Sample Code

# Include
  
   
Using namespace std; class Solution {public: // time complexity O (1) int climbStairs1 (const int n) {const double sqrtNum = sqrt (5 ); return int (floor (pow (1 + sqrtNum)/2, n + 1)-pow (1-sqrtNum)/2, n + 1 )) /sqrtNum);} // time complexity O (n) int climbStairs2 (const int n) {int current = 1; int last = 0; for (int I = 1; I <= n; I ++) {int temp = current; current + = last; last = temp;} return current ;}};
  

Simple test code:

#include ClimbingStairs.h#include 
  
   int main(){    int n;    cout << How many stairs?  << Input: ;    cin >> n;    Solution s;    int result1 = s.climbStairs1(n);    int result2 = s.climbStairs2(n);    cout << How many ways to reach the finish line?  Result1: << result1 << endl;    cout << How many ways to reach the finish line?  Result2: << result2 << endl;    system(pause);    return 0;}
  

Vulnerability. Because the generic formula uses floating-point operations and there are still physical books, the accuracy of the results cannot be guaranteed. In the book "The beauty of programming", we also provide a solution to divide governance strategies. This algorithm can achieve time complexity O (Log2n), while some blog posts on the Internet have written seven methods to describe the Fibonacci sequence, and further research is required.

 

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.