Skip to content

Commit cbf7e6f

Browse files
authored
Create 70_Climbing_Stairs.md
1 parent 395be78 commit cbf7e6f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

70_Climbing_Stairs.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
```

0 commit comments

Comments
 (0)