1. Small stairs
There are n ways for 1 or 2 stairs.
if (n <= 2) return nTyporithm · easy
Step 1 of 8 · intro card
Dynamic Programming
Climbing Stairs is Fibonacci with a story: each step can be reached from one or two steps below.
The state is the number of ways to reach a stair.
The transition adds the previous two states.
Only two variables are needed because older states are never read again.
State: ways to reach step i.
Base: step 1 has 1 way, step 2 has 2 ways.
Transition: ways[i-1] + ways[i-2].
Space can be O(1).
function climbStairs(n) {
if (n <= 2) return n;
let oneStepBefore = 2;
let twoStepsBefore = 1;
for (let step = 3; step <= n; step++) {
const current = oneStepBefore + twoStepsBefore;
twoStepsBefore = oneStepBefore;
oneStepBefore = current;
}
return oneStepBefore;
}Connect each part of the code to the concept before you type.
There are n ways for 1 or 2 stairs.
if (n <= 2) return nWays to reach step 2.
let oneStepBefore = 2Compute each next stair count from earlier counts.
for (let step = 3; step <= n; step++)Reach current from one step below or two steps below.
const current = oneStepBefore + twoStepsBeforeRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
Inputn = 6
Follow temporary values, local state and supporting structures at this step.
↗ The 7-cell working table above is itself auxiliary memory. Input values are shown separately.
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state