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?
Examples
Example 1 Input:n = 2
Output:2
Explanation: 1+1 or 2. Two ways.
Example 2 Input:n = 3
Output:3
Explanation: 1+1+1, 1+2, or 2+1. Three ways.
Constraints
▪1 <= n <= 45
Hints
Hint 1Show
This is the Fibonacci sequence in disguise.
Hint 2Show
ways(n) = ways(n-1) + ways(n-2)
Hint 3Show
Base cases: ways(1) = 1, ways(2) = 2
Starter Code
Solution.cpp
1class Solution {
2public:
3 int climbStairs(int n) {
4 // Your code here
5 return 0;
6 }
7};
Solution
Solve the problem first before reviewing the solution!