https://leetcode.com/problems/climbing-stairs/
class Solution {
public:
int climbStairs(int n) {
int a = 1, b = 1, temp = 0;
for(int i = 2; i <= n; i++) {
temp = a;
a = a + b;
b = temp;
}
return a;
}
};
References
https://leetcode.com/problems/climbing-stairs/discuss/2087468/Easy-c%2B%2B-solution
https://leetcode.com/problems/climbing-stairs/discuss/2091389/100-Beginner-Friendly-Explanation
best
class Solution {
public:
int climbStairs(int n) {
int dp[n+1];
dp[0]=1; dp[1]=1;
for(int i=2;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n];
}
};
728x90
반응형
'자료구조 알고리즘 > 코딩테스트' 카테고리의 다른 글
1581. Customer Who Visited but Did Not Make Any Transactions (0) | 2022.06.03 |
---|---|
746. Min Cost Climbing Stairs (0) | 2022.05.31 |
176. Second Highest Salary (0) | 2022.05.30 |
608. Tree Node (0) | 2022.05.30 |
1795. Rearrange Products Table (0) | 2022.05.30 |