11#!/usr/bin/env python3
22
33
4- def climb_stairs (n : int ) -> int :
4+ def climb_stairs (number_of_steps : int ) -> int :
55 """
66 LeetCdoe No.70: Climbing Stairs
7- Distinct ways to climb a n step staircase where
8- each time you can either climb 1 or 2 steps.
7+ Distinct ways to climb a number_of_steps staircase where each time you can either
8+ climb 1 or 2 steps.
99
1010 Args:
11- n : number of steps of staircase
11+ number_of_steps : number of steps on the staircase
1212
1313 Returns:
14- Distinct ways to climb a n step staircase
14+ Distinct ways to climb a number_of_steps staircase
1515
1616 Raises:
17- AssertionError: n not positive integer
17+ AssertionError: number_of_steps not positive integer
1818
1919 >>> climb_stairs(3)
2020 3
@@ -23,18 +23,17 @@ def climb_stairs(n: int) -> int:
2323 >>> climb_stairs(-7) # doctest: +ELLIPSIS
2424 Traceback (most recent call last):
2525 ...
26- AssertionError: n needs to be positive integer, your input -7
26+ AssertionError: number_of_steps needs to be positive integer, your input -7
2727 """
2828 assert (
29- isinstance (n , int ) and n > 0
30- ), f"n needs to be positive integer, your input { n } "
31- if n == 1 :
29+ isinstance (number_of_steps , int ) and number_of_steps > 0
30+ ), f"number_of_steps needs to be positive integer, your input { number_of_steps } "
31+ if number_of_steps == 1 :
3232 return 1
33- dp = [0 ] * (n + 1 )
34- dp [0 ], dp [1 ] = (1 , 1 )
35- for i in range (2 , n + 1 ):
36- dp [i ] = dp [i - 1 ] + dp [i - 2 ]
37- return dp [n ]
33+ previous , current = 1 , 1
34+ for _ in range (number_of_steps - 1 ):
35+ current , previous = current + previous , current
36+ return current
3837
3938
4039if __name__ == "__main__" :
0 commit comments