LeetCode 70번 Climbing Stairs JavaScript

찌니월드·1일 전
0

항해 99클럽 TIL

목록 보기
6/7

문제

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?

문제 출처

70. Climbing Stairs

나의 풀이

/**
 * @param {number} n
 * @return {number}
 */
var climbStairs = function(n) {
    if (n <= 2) return n;

    const dp = [];
    dp[1] = 1;
    dp[2] = 2;

    for (let i = 3; i <= n; i++) {
        dp[i] = dp[i - 2] + dp[i - 1];
    }

    return dp[n];
};
profile
Front-End Developer

0개의 댓글

관련 채용 정보