File tree Expand file tree Collapse file tree 1 file changed +23
-10
lines changed Expand file tree Collapse file tree 1 file changed +23
-10
lines changed Original file line number Diff line number Diff line change 3333from typing import List
3434
3535class Solution :
36- # time and space O(n)
3736 def climbStairs (self , n : int ) -> int :
38- memo = [0 ]* (n + 1 )
39- return self .climb (n , memo )
37+ if n < 2 :
38+ return n
39+
40+ dp = [0 , 1 ]
41+ i = 2
42+ while i <= n + 1 :
43+ tmp = dp [1 ]
44+ dp [1 ] = dp [0 ] + dp [1 ]
45+ dp [0 ] = tmp
46+ i += 1
47+ return dp [1 ]
48+
49+ # time and space O(n)
50+ # def climbStairs(self, n: int) -> int:
51+ # memo = [0]*(n+1)
52+ # return self.climb(n, memo)
4053
41- def climb (self , n : int , memo : List [int ]) -> int :
42- if n <= 1 :
43- return 1
44- if memo [n ]:
45- return memo [n ]
46- memo [n ] = self .climb (n - 1 , memo ) + self .climb (n - 2 , memo )
47- return memo [n ]
54+ # def climb(self, n: int, memo: List[int]) -> int:
55+ # if n <= 1:
56+ # return 1
57+ # if memo[n]:
58+ # return memo[n]
59+ # memo[n] = self.climb(n-1, memo) + self.climb(n-2, memo)
60+ # return memo[n]
4861
4962 # bruteforce solution O(2^n)
5063 # def climbStairs(self, n: int) -> int:
You can’t perform that action at this time.
0 commit comments