[코테 풀이] Climbing Stairs

시내·2024년 6월 5일

Q_70) Climbing Stairs

출처 : https://leetcode.com/problems/climbing-stairs/
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?

class Solution {
    public int climbStairs(int n) {
         int[] memo = new int[46];
        memo[1] = 1;
        memo[2] = 2;
        int count = 3;
        while (count <= n) {
            memo[count] = memo[count - 1] + memo[count - 2];
            count++;
        }
        return memo[n];
    }
}

💫 Memoization (DP) 활용

  • memo[count] = memo[count - 1] + memo[count - 2];
profile
contact 📨 ksw08215@gmail.com

0개의 댓글