https://leetcode.com/problems/climbing-stairs/
문제
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
풀이
x[i] = x[i-1] + x[i-2]
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
# https://leetcode.com/problems/climbing-stairs/submissions/
class Solution:
def climbStairs(self, n: int) -> int:
if n == 0: return 0
dp = {}
dp[1] = 1
dp[2] = 2
for i in range(3, n+1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]