Created
August 21, 2020 05:11
Star
You must be signed in to star a gist
Leetcode 70. Climbing Stairs (https://www.InterviewRecipes.com/leetcode-70)
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) = {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