leet code - Climbing Stairs

skyepodium·2021년 8월 1일
0

오랜만에 DP

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        Solution s = new Solution();

        System.out.println(s.climbStairs(1));
    }
}

class Solution {
    public int climbStairs(int n) {

        if (n <= 2) return n;

        int[] d = new int[n+1];

        d[0] = 0;
        d[1] = 1;
        d[2] = 2;

        for(int i=3; i<=n; i++) {
            d[i] = d[i-1] + d[i-2];
        }
        return d[n];
    }
}
class Solution:
    def climbStairs(self, n: int) -> int:

        if n <= 2: return n

        d = [0 for _ in range(n + 1)]

        d[0] = 0
        d[1] = 1
        d[2] = 2

        for i in range(3, n + 1):
            d[i] = d[i - 1] + d[i - 2]

        return d[n]
profile
callmeskye

0개의 댓글