7. Climbing stairs

Given an integer n, there is a staircase with n steps, starting from the 0th step.

Determine the number of unique ways to reach the nth step, given that each move can be either 1 or 2 steps at a time.

Example 1:

Input: n = 2

Output: 2

Explanation:

There are 2 unique ways to climb to the 2nd step:

1) 1 step + 1 step

2) 2 steps

Example 2:

Input: n = 3

Output: 3

Explanation:

There are 3 unique ways to climb to the 3rd step:

1) 1 step + 1 step + 1 step

2) 2 steps + 1 step

3) 1 step + 2 steps

Now Your Turn!

Pick the correct output for the given input

Input: n = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 45

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int climbStairs(int n) {
 
}
};
Test Case

Input:

N