70. Climbing Stairs
Easy
Problem:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 stepSolution:
Fibonacci numbers
class Solution:
dp = collections.defaultdict(int)
def climbStairs(self, n: int) -> int:
if n <= 2:
return n
if self.dp[n]:
return self.dp[n]
self.dp[n] = self.climbStairs(n - 1) + self.climbStairs(n - 2)
return self.dp[n]Reference: NeetCode


Last updated