You are climbing a staircase. It takes n steps to reach the top. Each time you can climb 1 or 2 steps. How many distinct ways?
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?
Explanation: 1+1+1, 1+2, 2+1.
Invariant: Ways to reach step i equals sum of ways to reach step i-1 and step i-2.
When current state depends only on the previous 2 states, optimize space from O(N) to O(1) using two variables.
Bottleneck: Linear Fibonacci loop.
You are climbing a staircase. It takes n steps to reach the top. Each time you can climb 1 or 2 steps. How many distinct ways?
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?
Explanation: 1+1+1, 1+2, 2+1.
Invariant: Ways to reach step i equals sum of ways to reach step i-1 and step i-2.
When current state depends only on the previous 2 states, optimize space from O(N) to O(1) using two variables.
Bottleneck: Linear Fibonacci loop.
Initialized DP table with base cases: dp[1] = 1, dp[2] = 2. Optimal transition: dp[i] = dp[i-1] + dp[i-2].