mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
61f3119467
* f-string update rsa_cipher.py * f-string update rsa_key_generator.py * f-string update burrows_wheeler.py * f-string update non_recursive_segment_tree.py * f-string update red_black_tree.py * f-string update deque_doubly.py * f-string update climbing_stairs.py * f-string update iterating_through_submasks.py * f-string update knn_sklearn.py * f-string update 3n_plus_1.py * f-string update quadratic_equations_complex_numbers.py * f-string update nth_fibonacci_using_matrix_exponentiation.py * f-string update sherman_morrison.py * f-string update levenshtein_distance.py * fix lines that were too long
44 lines
955 B
Python
44 lines
955 B
Python
#!/usr/bin/env python3
|
|
|
|
|
|
def climb_stairs(n: int) -> int:
|
|
"""
|
|
LeetCdoe No.70: Climbing Stairs
|
|
Distinct ways to climb a n step staircase where
|
|
each time you can either climb 1 or 2 steps.
|
|
|
|
Args:
|
|
n: number of steps of staircase
|
|
|
|
Returns:
|
|
Distinct ways to climb a n step staircase
|
|
|
|
Raises:
|
|
AssertionError: n not positive integer
|
|
|
|
>>> climb_stairs(3)
|
|
3
|
|
>>> climb_stairs(1)
|
|
1
|
|
>>> climb_stairs(-7) # doctest: +ELLIPSIS
|
|
Traceback (most recent call last):
|
|
...
|
|
AssertionError: n needs to be positive integer, your input -7
|
|
"""
|
|
assert (
|
|
isinstance(n, int) and n > 0
|
|
), f"n needs to be positive integer, your input {n}"
|
|
if n == 1:
|
|
return 1
|
|
dp = [0] * (n + 1)
|
|
dp[0], dp[1] = (1, 1)
|
|
for i in range(2, n + 1):
|
|
dp[i] = dp[i - 1] + dp[i - 2]
|
|
return dp[n]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|