-
Notifications
You must be signed in to change notification settings - Fork 0
/
070-Climbing_Stairs.py
67 lines (48 loc) · 1.47 KB
/
070-Climbing_Stairs.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
## Dynamic Programing
# Time: O(n)
# Space: O(n)
class Solution:
def climbStairs(self, n: int) -> int:
# dp[i] = dp[i-1] + dp[i-2]
dp = [0]*(n+1)
dp[0] = 1
dp[1] = 2
for i in range(2, n):
dp[i] = dp[i-1] + dp[i-2]
return dp[n-1]
## Fibonacci Number
# Time: O(n)
# Space: o(1)
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1: return 1
first = 1
second = 2
for i in range(2,n):
third = first + second
first = second
second = third
return second
## Brute Force
# Time: O(2^n)
# Space: O(n) The depth of the recursion tree can go upto nn.
class Solution:
def climbStairs(self, n: int) -> int:
def climb( i, n ):
if i>n: return 0
if i == n: return 1
return climb(1+i,n) + climb(2+i,n)
return climb(0,n)
## Recursion with Memoization
# Time: O(n)
# Space: O(n)
class Solution:
def climbStairs(self, n: int) -> int:
memo = [0]*(n+1)
def climb( i, n, memo):
if i > n: return 0
if i == n : return 1
if memo[i] > 0: return memo[i]
memo[i] = climb( i+1, n, memo ) + climb( i+2, n, memo )
return memo[i]
return climb( 0, n, memo )