Climbing Stairs
ID: 70; easy
Solution 1
The DP function is:
dp[i]
stands for the number of ways to reach the ith step. This number should be equal to the number of ways to reach the (i-1)th step plus the number of ways to reach the (i-2)th step. The reason is that one can only take 1 or 2 steps, so the ith step either comes from the (i-1)th step or the (i-2)th step.
Time complexity: O(n)
Space complexity: O(n)
Solution 2
It is not hard to tell that this DP function resembles the Fibonacci Number function. Then, we can use what we have in the Fibonacci Number problem (use 3 variables to dynamically keep the results) to solve this problem.
Time complexity: O(n)
Space complexity: O(1)
Last updated