-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path70.py
28 lines (22 loc) · 794 Bytes
/
70.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
# 70. Climbing Stairs
# Solved
# Easy
# Topics
# Companies
# Hint
# You are climbing a staircase. It takes n steps to reach the top.
# Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
class Solution:
def climbStairs(self, n: int) -> int:
memo = {}
return self.helper(n, memo)
def helper(self, n: int, memo: dict[int, int]) -> int:
if n == 0 or n == 1:
return 1
if n not in memo:
memo[n] = self.helper(n-1, memo) + self.helper(n-2, memo)
return memo[n]
# Time: O(n)
# Space: O(n)
# Runtime: 32 ms, faster than 50.50% of Python3 online submissions for Climbing Stairs.
# Memory Usage: 14.4 MB, less than 11.11% of Python3 online submissions for Climbing Stairs.