File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ ## 70. Climbing Stairs
2+
3+
4+ You are climbing a staircase. It takes n steps to reach the top.
5+
6+ Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
7+
8+
9+
10+ Example 1:
11+
12+ Input: n = 2
13+ Output: 2
14+ Explanation: There are two ways to climb to the top.
15+ 1 . 1 step + 1 step
16+ 2 . 2 steps
17+ Example 2:
18+
19+ Input: n = 3
20+ Output: 3
21+ Explanation: There are three ways to climb to the top.
22+ 1 . 1 step + 1 step + 1 step
23+ 2 . 1 step + 2 steps
24+ 3 . 2 steps + 1 step
25+
26+
27+ Constraints:
28+
29+ 1 <= n <= 45
30+
31+
32+ ``` python
33+ def climbStairs (self , n : int ) -> int :
34+
35+ dp = [0 ] * (n+ 1 )
36+ dp[0 ] = 1
37+ dp[1 ] = 1
38+ for i in range (2 , n+ 1 ):
39+ dp[i] = dp[i- 1 ] + dp[i- 2 ]
40+ return dp[- 1 ]
41+ ```
42+
43+ ```
44+ Runtime: 20 ms, faster than 98.10% of Python3 online submissions for Climbing Stairs.
45+ Memory Usage: 14.3 MB, less than 10.74% of Python3 online submissions for Climbing Stairs.
46+ ```
You can’t perform that action at this time.
0 commit comments