Skip to content

Commit

Permalink
Climbing stairs sol
Browse files Browse the repository at this point in the history
  • Loading branch information
oyeong011 committed Dec 17, 2024
1 parent 626d390 commit bfa66f6
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions climbing-stairs/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int climbStairs(int n) {
if(n == 0 || n == 1){
return 1;
}
vector<int> dp(n + 1);
dp[0] = dp[1] = 1;
for(int i = 2; i <= n; i++){
dp[i] = dp[i-1] + dp[i - 2];
}
return dp[n];
}
};

// dp ๋ฌธ์ œ๋กœ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์ค€๋‹ค
// ๊ณ„๋‹จ ๋ฐฉ์‹
// 1 1๊ฐœ
// 1 + 1, 2 2๊ฐœ
// 1 + 1 + 1, 2 + 1, 1 + 2 3๊ฐœ
// 1 + 1 + 1 + 1, 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1, 2 + 2 ---> 5๊ฐœ
// ์ฆ‰ ํ”ผ๋ณด๋‚˜์ฐŒ ์ˆ˜์—ด์˜ ํ˜•ํƒœ๋ฅผ ๋ˆ๋‹ค ์™œ๋ƒ๋ฉด 2์นธ์„ ๋›ฐ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ์นธ์ „์— 1์„ ๋”ํ•˜๊ณ  ๋‘์นธ ์ „์— ๊ฒฝ์šฐ์˜ ์ˆ˜์— 2๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋˜๊ธฐ๋•Œ๋ฌธ
// ์ฆ‰ f(n) = f(n - 1) + f(n - 2)
// ๋งŒ์•ฝ ๊ณ„๋‹จ ๋ฐฉ์‹์ด 3์นธ๊นŒ์ง€์ด๋ฉด ์ฆ‰ f(n) = f(n - 1) + f(n - 2) + f(n - 3)
// ์‹œ๊ฐ„๋ณต์žก๋„๋Š” n๋งŒํผ ์ˆœํšŒํ•˜์—ฌ O(n)

0 comments on commit bfa66f6

Please sign in to comment.