-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_0070.py
36 lines (27 loc) · 985 Bytes
/
leetcode_0070.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution:
def __init__(self):
self.stairs_sequence: list = [1, 2] # Base cases
def climbStairs(self, n: int) -> int:
if n <= 0:
return 1 # There is 1 way to stay at the ground (by doing nothing)
if len(self.stairs_sequence) >= n:
return self.stairs_sequence[n - 1]
prev, current = self.stairs_sequence[-2], self.stairs_sequence[-1]
for _ in range(len(self.stairs_sequence), n):
prev, current = current, prev + current
self.stairs_sequence.append(current)
return self.stairs_sequence[n - 1]
s = Solution()
print(s.climbStairs(n=8))
# Best of Code Readability
# class SolutionFib:
# def climbStairs(self, n: int) -> int:
# if n <= 2:
# return n
# current = 1
# prev = 1
# for i in range(n-1):
# current, prev = current + prev, current
# return current
# s = SolutionFib()
# print(s.climbStairs(7))