[LeetCode] Climbing Stairs

70. Climbing Stairs

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

1
2
3
4
5
6
7
class Solution {
int dp[46] = {0,};
public:
int climbStairs(int n) {
return n <= 2 ? n : dp[n] ? dp[n] : dp[n] = climbStairs(n-2) + climbStairs(n-1);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/02/PS/LeetCode/climbing-stairs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.