Advertisement
smj007

Untitled

Feb 13th, 2024
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. class Solution:
  2.     def climbStairs(self, n: int) -> int:
  3.  
  4.         # better way of dealing with base case
  5.         if n == 1:
  6.             return 1
  7.         if n == 2:
  8.             return 2
  9.  
  10.         dp = [0]*n
  11.         dp[0] = 1
  12.         dp[1] = 2
  13.  
  14.         for i in range(2, n):
  15.             dp[i] = dp[i-1] + dp[i-2]
  16.  
  17.         return dp[-1]
  18.  
  19. class Solution:
  20.     def climbStairs(self, n: int) -> int:
  21.  
  22.         if n == 1:
  23.             return 1
  24.         if n == 2:
  25.             return 2
  26.  
  27.         n0 = 1
  28.         n1 = 2
  29.  
  30.         for i in range(2, n):
  31.             count = n0 + n1
  32.             n0 = n1
  33.             n1 = count
  34.  
  35.         return count
  36.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement