Add soltuion for neetcode: staircase
This commit is contained in:
parent
bd2d55fafe
commit
24d6a5c0f3
2 changed files with 40 additions and 0 deletions
26
neetcode/recursion/climbing_stairs/README.md
Normal file
26
neetcode/recursion/climbing_stairs/README.md
Normal file
|
@ -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
|
14
neetcode/recursion/climbing_stairs/solution.py
Normal file
14
neetcode/recursion/climbing_stairs/solution.py
Normal file
|
@ -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))
|
Loading…
Reference in a new issue