Skip to content

Commit 2c96f62

Browse files
authored
Update Climbing_Stairs.py
1 parent 1c79ce9 commit 2c96f62

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Climbing_Stairs/Climbing_Stairs.py

+23
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1+
class Solution(object):
2+
def climbStairs(self, n):
3+
"""
4+
Given a number n, return the number of distinct ways to climb to the top of a staircase with n steps.
5+
6+
:type n: int
7+
:rtype: int
8+
"""
9+
if n == 1:
10+
return 1
11+
elif n == 2:
12+
return 2
113

14+
prev1 = 1 # Base case: the number of ways to reach the first step
15+
prev2 = 2 # Base case: the number of ways to reach the second step
16+
current = 0
17+
18+
# Start from step 3 and calculate the number of ways to reach each subsequent step
19+
for i in range(3, n + 1):
20+
current = prev1 + prev2 # The number of ways to reach the current step is the sum of the previous two
21+
prev1 = prev2 # Update prev1 to be the previous step
22+
prev2 = current # Update prev2 to be the current step
23+
24+
return current

0 commit comments

Comments
 (0)