Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bhaveshmunot1
Created June 4, 2020 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaveshmunot1/ebb28040e7223839feef4c1cebb5481f to your computer and use it in GitHub Desktop.
Save bhaveshmunot1/ebb28040e7223839feef4c1cebb5481f to your computer and use it in GitHub Desktop.
Leetcode #70: Climbing Stairs
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