C++ Interview Guide Question 1: Climbing the Stairs

topic

        When Xiaole climbs stairs, he can only go up 1 or 2 steps at a time. There are n steps in the stairs. How many ways are there to climb up the stairs?

parse

        Although this question is a programming question, it is actually a mathematics question, which focuses on testing the applicant's logical thinking ability and analytical problem-solving ability.

        When there is only 1 level of stairs, there is only 1 way to go upstairs.

        When there are 2 steps on the stairs, there are two ways to go upstairs: one step at a time, or two steps at a time.

        For stairs larger than 2 levels, we can choose to cross 1 level from level n-1, or cross 2 levels from level n-2. Therefore, the number of methods for n steps of stairs is: f(n) = f(n-1) + f(n-2).

        This way of thinking about problems is recursive thinking. Below, we give example code that uses a recursive function to solve this problem.

#include <iostream>
using namespace std;

unsigned int CalcUpstairsCount(unsigned int uiSteps)
{
    if (uiSteps <= 2)
    {
        return uiSteps;
    }

    return CalcUpstairsCount(uiSteps - 1) + CalcUpstairsCount(uiSteps - 2);
}

int main()
{
    cout << CalcUpstairsCount(0) << endl;
    cout << CalcUpstairsCount(1) << endl;
    cout << CalcUpstairsCount(2) << endl;
    cout << CalcUpstairsCount(3) << endl;
    cout << CalcUpstairsCount(4) << endl;
    cout << CalcUpstairsCount(5) << endl;
    getchar();
    return 0;
}

        As you can see, the code implemented using recursion is very concise. However, the recursive algorithm also has its shortcomings: first, when there are many levels of recursion, it may cause stack overflow; second, the time complexity of the recursive algorithm is generally high and the efficiency is low. Specific to this question, because each recursion may produce two choices, the time complexity is O(2^n). When calculating the number of methods for n-level steps and n-1 steps, the number of methods for n-2 steps will be calculated, resulting in a large number of repeated calculations.

        Is there a better way to solve the problem? The answer is yes, we can try it through dynamic programming algorithm.

        We can use an array dp to store the number of methods for each staircase. The initial conditions are: dp[1] = 1, dp[2] = 2.

        For stairs larger than 2 levels, we can cross 1 level from level n-1 to level n, or cross 2 levels from level n-2 to level n. Therefore, dp[n] = dp[n-1] + dp[n-2].

        In this way, we can calculate the number of methods for all stair steps in one traversal. The time complexity of this method is O(n), and the space complexity is also O(n). Below, we give an example code that uses the dynamic programming algorithm to solve this problem.

#include <iostream>
#include <vector>
using namespace std;

unsigned int CalcUpstairsCount(unsigned int uiSteps)
{
    if (uiSteps <= 2)
    {
        return uiSteps;
    }

    // 为了方便理解,第0个元素未使用
    vector<int> vctCount(uiSteps + 1, 0);
    vctCount[1] = 1;
    vctCount[2] = 2;
    for (unsigned int i = 3; i <= uiSteps; i++)
    {
        vctCount[i] = vctCount[i - 1] + vctCount[i - 2];
    }

    return vctCount[uiSteps];
}

int main()
{
    cout << CalcUpstairsCount(0) << endl;
    cout << CalcUpstairsCount(1) << endl;
    cout << CalcUpstairsCount(2) << endl;
    cout << CalcUpstairsCount(3) << endl;
    cout << CalcUpstairsCount(4) << endl;
    cout << CalcUpstairsCount(5) << endl;
    getchar();
    return 0;
}

Summarize

        Through this question, we learned to use recursive algorithms and dynamic programming algorithms to program and solve problems. The time complexity of the recursive algorithm is high, and the time complexity of the dynamic programming algorithm is low.

        After studying the above sample code, do you really understand the recursive algorithm and dynamic programming algorithm? We have left some after-school development homework for you, come and give it a try!

        ​ ​ 1. When Xiaole climbs stairs, he can only go up 1 step, 2 steps, or 3 steps at a time. There are n steps in the stairs. How many ways are there to climb up the stairs?

        ​ ​ ​ 2. There are small grids with length and width of 1x1 and 1x2 respectively. Now we need to use these two small grids to splice them into a large grid of 1xN. Please write a method to calculate how many splicing solutions there are. N is the only parameter of the method, and the return value is the number of splicing solutions.

Guess you like

Origin blog.csdn.net/hope_wisdom/article/details/134769674