- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_070.py
More file actions
Latest commit
19 lines (15 loc) · 611 Bytes
/
Copy pathproblem_070.py
File metadata and controls
19 lines (15 loc) · 611 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""
Problem: https://leetcode.com/problems/climbing-stairs/
Solution: Number of ways to reach a step n would be sum of number of ways to reach n-1 and n-2 steps.
Time Complexity: O(n) as we're iterating n times.
Space Complexity: O(1) as we're using a constant space data structure to keep tarck of steps.
"""
classSolution:
defclimbStairs(self, n: int) ->int:
ifn<=2:
returnn
step_tracker= [1, 2]
foriinrange(n-2):
step_tracker.append(step_tracker[0] +step_tracker[1])
delstep_tracker[0]
returnstep_tracker[-1]