> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Climbing Stairs

Leetcode Problem 70: Climbing Stairs

Problem Summary

You are climbing a staircase that has n steps. Each time you can climb either 1 or 2 steps. Return the number of distinct ways you can reach the top. The number of steps n is a positive integer at most 45.

Techniques

  • Math
  • Dynamic Programming
  • Memoization

Solution

function climbStairs(n: number): number {
    let one = 1
    let two = 1

    for (let i = 0; i < n - 1; i++) {
        const tempOne = one
        one = one + two
        two = tempOne
    }

    return one
};

console.log(climbStairs(2)) // 2
console.log(climbStairs(3)) // 3
console.log(climbStairs(4)) // 5
console.log(climbStairs(5)) // 8