Created
June 24, 2020 08:45
-
-
Save Irene-123/e835b5110c0df00797a53e8c18999d98 to your computer and use it in GitHub Desktop.
70. Climbing Stairs Leetcode Python solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This problem has typically 3-4 approaches .I'm gonna show two of them here . | |
1. DYANAMIC PROGRAMMING APPROACH . | |
```python3 | |
class Solution: | |
def climbStairs(self, n: int) -> int: | |
if n==0: | |
return 0 | |
if n==1: | |
return 1 | |
dp=[0]*(n+1) | |
dp[1]=1 | |
dp[2]=2 | |
for i in range (3,n+1): | |
dp[i]=dp[i-1]+dp[i-2] | |
return dp[n] | |
``` | |
2. RECURSION WITH MEMOISATION | |
```python3 | |
class Solution: | |
def climbStairs(self, n: int) -> int: | |
memo=[0]*(n+1) | |
def recursion(i,n ,memo): | |
if i>n: | |
return 0 | |
if i==n: | |
return 1 | |
if memo[i]>0: | |
return memo[i] | |
memo[i]=recursion(i+1,n,memo)+recursion(i+2,n,memo) | |
return memo[i] | |
return recursion(0,n,memo) | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment