DSA Patterns (Multi-Lang)•Part 11: Dynamic Programming & Knapsack (91 - 100)
Climbing Stairs.
In Simple Words: Fibonacci recurrence: to reach step $n$, you must come from step $n-1$ (1 step) or step $n-2$ (2 steps). Track only last two steps in $O(1)$ space.
- Time: $O(N)$ | Space: $O(1)$
Multi-Language SolutionsPython 3 • JavaScript • TypeScript • Java • C++
Switch tabs or choose Alldef climbStairs(n: int) -> int:
one, two = 1, 1
for _ in range(n - 1):
temp = one
one = one + two
two = temp
return one