InterviewHubPRO
Section Overview
Q91

Question 91 of 100

Part 11: Dynamic Programming & Knapsack (91 - 100)

91%
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 Solutions
Switch tabs or choose All
def climbStairs(n: int) -> int:
    one, two = 1, 1
    for _ in range(n - 1):
        temp = one
        one = one + two
        two = temp
    return one