반응형

https://leetcode.com/problems/climbing-stairs/description

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

class Solution {
public:
    int climbStairs(int n) {
        if (n <= 1) return 1;
        int dp[3] = {1, 1, 0};
        for(int i=2; i<=n; i++) {
            dp[2] = dp[0] + dp[1];
            dp[0] = dp[1];
            dp[1] = dp[2];  
        }
        return dp[2];
    }
};
반응형

+ Recent posts