Stair Lantern Routes
You are climbing a staircase and can move either 1 step or 2 steps at a time. The question is how many different routes can reach exactly the top.
Order matters here. Taking 1 + 2 is a different route from taking 2 + 1, because they happen in a different sequence.
For example, if steps = 5, the answer is 8. If steps = 3, the routes are 1+1+1, 1+2, and 2+1, so the answer is 3. If steps = 0, the answer is 1 because doing nothing is one valid way to already be at the top.
So the task is to count every ordered sequence of 1-step and 2-step moves that lands exactly on the final stair.
Example Input & Output
Eight ordered combinations of single and double steps reach the fifth stair.
Staying at the floor counts as one valid plan.
Three plans exist: 1+1+1, 1+2, and 2+1.
Algorithm Flow

Solution Approach
This is one of the simplest dynamic programming patterns. Let dp[n] be the number of ways to reach step n.
To arrive at step n, the last move must have come from either step n - 1 with a 1-step jump or from step n - 2 with a 2-step jump. So the recurrence is:
The base values are:
dp[0] = 1
dp[1] = 1
From there, you fill upward until the requested number of steps. This is why the problem follows the Fibonacci pattern.
You can store the whole DP array, or just keep the previous two values to get O(1) extra space.
Best Answers
class Solution {
public int calculate_climb_ways(int n) {
if (n <= 2) return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = a + b;
a = b; b = tmp;
}
return b;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
