Climbing Stairs

LeetCode

LeetCode: https://leetcode.com/problems/climbing-stairs/

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

1def climbStairs(n: int) -> int:
2 if n <= 2:
3 return n
4 a, b = 1, 2
5 for _ in range(3, n + 1):
6 a, b = b, a + b
7 return b
1
0
2
1
n=5
a=1
b=2
Step 1 / 5
Step 1:
Use dp[n]=dp[n-1]+dp[n-2]. For n=5, initialize a=dp[1]=1 and b=dp[2]=2.
Pointers: i=2
Focus: select @ [0, 1]
n=5a=1b=2