Created
June 4, 2020 03:38
-
-
Save bhaveshmunot1/ebb28040e7223839feef4c1cebb5481f to your computer and use it in GitHub Desktop.
Leetcode #70: Climbing Stairs
This file contains 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
class Solution { | |
public: | |
int climbStairs(int n) { | |
vector<int> dp(n+1); | |
dp[1] = 1; | |
dp[2] = 2; | |
for (int i=3; i<=n; i++) { | |
dp[i] = dp[i-1] + dp[i-2]; | |
} | |
return dp[n]; | |
} | |
}; | |
// Note: Space complexity of this solution can be reduced to O(1) by using 2-3 variables instead of an array/vector. | |
// As this generates a Fibonacci series, you can use a relatively more complex but efficient way of finding Nth Fibonacci number instead of using the dynamic programming approach. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment