Climbing Stairs
Count ordered ways to reach target steps using hops of one or two. The values list is unused.
EXECUTION LAB
Count ordered ways to reach target steps using hops of one or two. The values list is unused.
Input and constraints
- Target must be between 0 and 20.
- Use at most 32 integers. Each numeric input is between -1000 and 1000.
Code inspector
def solve(values, target): prev = 1 curr = 1 i = 2 while (i <= target): ways = (prev + curr) prev = curr curr = ways i = (i + 1) return currVisualizer
Step 1 of 6Empty array
Combine the two predecessor route counts
Combine the two predecessor route counts.
Variable watch
i 2prev 1curr 1target 6
Step explanation
Combine the two predecessor route counts.
comparisons 1reads 0writes 0swaps 0recursiveCalls 0
Educational simulation · O(t) time · O(1) space
Worked explanation
Every route to a landing arrives from one of the previous two landings.
Guided solution
Each route ends with either a one-step or two-step hop. Add the counts at those two predecessor stairs.
O(t) time · O(1) auxiliary space
Examples
Worked example · {"values":[],"target":6} ⇒ 13
Edge case 1 · {"values":[],"target":0} ⇒ 1
Edge case 2 · {"values":[],"target":1} ⇒ 1
Progressive hints
Hint 1
Every route to a landing arrives from one of the previous two landings.Hint 2
Each route ends with either a one-step or two-step hop. Add the counts at those two predecessor stairs.Hint 3
Step through the worked example and inspect the highlighted variables before each update.Common mistake
The starting floor has one empty route, so ways(0) equals one.
Interview follow-up
What if three-step hops were allowed?