Skip to content

Instantly share code, notes, and snippets.

@bhaveshmunot1
Created August 21, 2020 05:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bhaveshmunot1/8708cf53f238738e10db8cd7dc615cdb to your computer and use it in GitHub Desktop.
Leetcode 70. Climbing Stairs (https://www.InterviewRecipes.com/leetcode-70)
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n+1) = {0, 1, 2}; // Stores number of ways in which
// i-th step can be reached, at
// location dp[i].
for (int i=3; i<=n; i++) { // For each step - (bottom to top)
dp[i] = dp[i-1] + dp[i-2]; // All the ways in which (i-1)-th
// step can be reached AND all the
// ways in which (i-2)-th step can
// be reached
}
return dp[n]; // Return answer.
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment