Daily Algorithm: Stair Climbing Problem

Daily Algorithm: Stair Climbing Problem

[[433205]]

Suppose you are climbing a staircase. It takes n steps for you to reach the top.

You can climb 1 or 2 steps at a time. How many different ways can you get to the top of the building?

Note: Given n is a positive integer.

Example 1:

  1. Input: 2
  2. Output: 2
  3. Explanation: There are two ways to get to the roof.
  4. 1. 1st order + 1st order
  5. 2. 2nd order

Example 2:

  1. Input: 3
  2. Output: 3
  3. Explanation: There are three ways to climb to the roof.
  4. 1. 1st order + 1st order + 1st order
  5. 2. 1st order + 2nd order
  6. 3. 2nd order + 1st order

Solution: Dynamic Programming

Dynamic Programming (DP) is a strategy for breaking down complex problems into small problems for solution. However, unlike the divide-and-conquer algorithm, which requires each sub-problem to be independent of each other, the sub-problems of dynamic programming are interrelated.

Divide and conquer, as the name suggests, is to divide and conquer. It divides a complex problem into two or more similar sub-problems, and then divides the sub-problems into smaller sub-problems until the smaller sub-problems can be easily solved. When the sub-problems are solved, the solution to the original problem is the combination of the solutions to the sub-problems.

When we use dynamic programming to solve a problem, we need to follow several important steps:

  • Defining subproblems
  • Implement the parts of the sub-problem that need to be solved repeatedly
  • Identify and solve for boundary conditions

Step 1: Define the subproblems

If we use dp[n] to represent the number of options for the nth step, and we know from the question that the last step may be 2 steps or 1 step, then the number of options for the nth step is equal to the number of options for the n-1th step plus the number of options for the n-2th step.

Step 2: Implement the sub-problems that need to be solved repeatedly

  1. dp[n] = dp[n−1] + dp[n−2]

Step 3: Identify and solve for boundary conditions

  1. // Level 0, 1st option
  2. dp[0]=1
  3. // Level 1 is also 1 solution
  4. dp[1]=1

The last step: translate the tail code into code and handle some edge cases

  1. let climbStairs = function (n) {
  2. let dp = [1, 1]
  3. for (let i = 2; i <= n; i++) {
  4. dp[i] = dp[i - 1] + dp[i - 2]
  5. }
  6. return dp[n]
  7. }

Complexity analysis:

  • Time complexity: O(n)
  • Space complexity: O(n)

Optimize space complexity:

  1. let climbStairs = function (n) {
  2. let res = 1, n1 = 1, n2 = 1
  3. for (let i = 2; i <= n; i++) {
  4. res = n1 + n2
  5. n1 = n2
  6. n2 = res
  7. }
  8. return res
  9. }

Space complexity: O(1)

leetcode: https://leetcode-cn.com/problems/climbing-stairs/solution/pa-lou-ti-wen-ti-by-user7746o/

<<:  Next generation WiFi: There is still signal one kilometer away!

>>:  Several thinking patterns that need to be changed in the 6G era

Recommend

About remote procedure call gRPC

If you have been exposed to distributed systems, ...

Let’s talk about what CDN is. Do you know these characteristics?

Preface When we browse the web, watch videos, and...

spinservers San Jose China Telecom network independent server simple test

A few days ago, the tribe shared the test informa...

WiFi speed is slow, try these 8 simple tips

Slow WiFi speed is always a headache, especially ...

Today, China’s 5G is two years old!

On October 31, 2019, the first day of the 28th Ch...

Verizon and Honda collaborate on 5G and edge computing to make driving safer

According to foreign media reports, Honda and tel...

Threat attacks targeting home routers increased fivefold

In the first quarter of 2018, the number of cyber...

Let’s listen to what 5G R18 is talking about?

[[400274]] This article is reprinted from the WeC...