70.Climbing Stairs
1.Description(Easy)
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?
Example
Given an example n=3 , 1+1+1=2+1=1+2=3
return 3.
2.Code
(高频)统计方案个数+一维坐标
state: f(n) 表示爬了n 步后有几种爬法。-->开int[n+1] 返回f(n)
function:f(n)=f(n-1)+f(n-2);
Initialize: f(0)=1, f(1)=1;
Answer: f(n)
Approach 1: Recursion
Explanation: The recursive solution uses the concept of Fibonacci numbers to solve the problem. It calculates the number of ways to climb the stairs by recursively calling the climbStairs
function for (n-1) and (n-2) steps. However, this solution has exponential time complexity (O(2^n)) due to redundant calculations.
Approach 2: Memoization
Explanation: The memoization solution improves the recursive solution by introducing memoization, which avoids redundant calculations. We use an unordered map (memo
) to store the already computed results for each step n
. Before making a recursive call, we check if the result for the given n
exists in the memo. If it does, we return the stored value; otherwise, we compute the result recursively and store it in the memo for future reference
Approach 3: Tabulation
Explanation: The tabulation solution eliminates recursion and uses a bottom-up approach to solve the problem iteratively. It creates a DP table (dp
) of size n+1 to store the number of ways to reach each step. The base cases (0 and 1 steps) are initialized to 1 since there is only one way to reach them. Then, it iterates from 2 to n, filling in the DP table by summing up the values for the previous two steps. Finally, it returns the value in the last cell of the DP table, which represents the total number of ways to reach the top.
Approach 4: Space Optimization
Explanation: The space-optimized solution further reduces the space complexity by using only two variables (prev
and curr
) instead of an entire DP table. It initializes prev
and curr
to 1 since there is only one way to reach the base cases (0 and 1 steps). Then, in each iteration, it updates prev
and curr
by shifting their values. curr
becomes the sum of the previous two values, and prev
stores the previous value of curr
.
Last updated
Was this helpful?