Climbing Stairs

Easy Subject: Dynamic Programming
Time Complexity
O(N)
Space Complexity
O(1)

Problem Description

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Optimal Solution

Python
def climbStairs(n):
    if n <= 2:
        return n
    one, two = 1, 2
    for _ in range(3, n + 1):
        temp = one + two
        one = two
        two = temp
    return two