๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Climbing Stairs

by dkswnkk 2021. 11. 9.

๋ฌธ์ œ

 

Climbing Stairs - LeetCode

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) {
        int dp[46];

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

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

 

ํ’€์ด

๊ฐ„๋‹จํ•œ dp ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ฒซ ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” (1์นธ์”ฉ ์˜ฌ๋ผ๊ฐ€๊ธฐ) ํ•œ ๊ฐ€์ง€์ด๊ณ  ๋‘ ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š”(1์นธ+1์นธ, 2์นธ)์œผ๋กœ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์„ค์ •์„ ํ•ด์ฃผ๊ณ  ์„ธ ๋ฒˆ์งธ ์นธ๋ถ€ํ„ฐ์˜ ๊ณ„๋‹จ์€ ์ด์ „์— ํ•œ ์นธ์œผ๋กœ ์˜ฌ๋ผ์™”์„ ๊ฒฝ์šฐ์™€ ๋‘ ์นธ์œผ๋กœ ์˜ฌ๋ผ์˜จ ๊ฒฝ์šฐ๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

'Algorithm ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป > Leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leetcode,c++] Longest Common Prefix  (0) 2021.11.11
[Leetcode,c++] Permutations  (0) 2021.11.10
[Leetcode,c++] Best Time to Buy and Sell Stock  (0) 2021.11.10
[Leetcode,c++] Maximum Subarray  (0) 2021.11.09
[Leetcode,c++] Valid Parentheses  (0) 2021.11.09
[Leetcode,c++] Palindrome Number  (0) 2021.11.09

๋Œ“๊ธ€