NOTE

Climbing Stairs

Count the ways to climb stairs using a Fibonacci recurrence.

Data Structures & AlgorithmsCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Problem Description

Suppose you are climbing stairs. It takes n steps to reach the top.

Each time you can climb 1 or 2 steps. How many different ways can you reach the top?

2. Approach

  1. Approach 1
    • Fibonacci

3. Implementation

3.1. Fibonacci

func climbStairs(n int) int {
    if n <= 2 {
        return n
    }

    a := 1
    b := 2
    c := a + b
    for i := 3; i <= n ; i++ {
        c = a + b
        a = b
        b = c
    }
    return c
}

4. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub