Leetcode 70. Climbing Stairs (Easy) (cpp)
Tag: Dynamic Programming
Difficulty: Easy
/*
70. Climbing Stairs (Easy)
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
*/
class Solution {
public:
int climbStairs(int n) {
if (n < 4) return n;
int i = 4, prev1 = 2, prev2 = 3;
while (i <= n) {
int temp = prev2;
prev2 += prev1;
prev1 = temp;
i++;
}
return prev2;
}
};