diff --git a/neetcode/recursion/climbing_stairs/README.md b/neetcode/recursion/climbing_stairs/README.md new file mode 100644 index 0000000..f5ebc71 --- /dev/null +++ b/neetcode/recursion/climbing_stairs/README.md @@ -0,0 +1,26 @@ +You are given an integer n representing the number of steps to reach the top of a staircase. You can climb with either 1 or 2 steps at a time. + +Return the number of distinct ways to climb to the top of the staircase. + +Example 1: + +Input: n = 2 + +Output: 2 +Explanation: + +1 + 1 = 2 +2 = 2 +Example 2: + +Input: n = 3 + +Output: 3 +Explanation: + +1 + 1 + 1 = 3 +1 + 2 = 3 +2 + 1 = 3 +Constraints: + +1 <= n <= 30 diff --git a/neetcode/recursion/climbing_stairs/solution.py b/neetcode/recursion/climbing_stairs/solution.py new file mode 100644 index 0000000..01361b4 --- /dev/null +++ b/neetcode/recursion/climbing_stairs/solution.py @@ -0,0 +1,14 @@ +class Solution: + def climbStairs(self, n: int) -> int: + if n == 0: + return 1 + if n < 0: + return 0 + return self.climbStairs(n - 1) + self.climbStairs(n - 2) + + +if __name__ == "__main__": + s = Solution() + print(s.climbStairs(5)) + print(s.climbStairs(30)) + print(s.climbStairs(1))