We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 20d4573 commit 3549a8cCopy full SHA for 3549a8c
โclimbing-stairs/sungjinwi.cpp
@@ -0,0 +1,31 @@
1
+/*
2
+ ํ์ด :
3
+ n์ด 1๊ณผ 2์ผ ๋๋ ๋ฐ๋ก ์ฒ๋ฆฌ, ๊ทธ ์ธ์ n๋ฒ์งธ๋ prv(n - 2๋ฒ์งธ) + cur(n -1๋ฒ์งธ)๋ก ๊ฐ์ ์ ๋ฐ์ดํธ ํ๋ฉฐ n๊น์ง ๋ํด๋๊ฐ
4
+
5
+ TC : O(N)
6
+ n์ ํฌ๊ธฐ์ ๋ฐ๋ณต๋ฌธ์ด ๋น๋กํ๋ค
7
8
+ SC : O(1)
9
+ n์ ํฌ๊ธฐ์ ์๊ด์์ด 3๊ฐ์ ๋ณ์ ์ฌ์ฉ
10
+*/
11
12
+class Solution {
13
+public:
14
+ int climbStairs(int n) {
15
+ if (n == 1)
16
+ return 1;
17
+ if (n == 2)
18
+ return 2;
19
20
+ int prv = 1;
21
+ int cur = 2;
22
+ int tmp;
23
+ for (int i = 3; i <= n; i++)
24
+ {
25
+ tmp = cur;
26
+ cur = cur + prv;
27
+ prv = tmp;
28
+ }
29
+ return cur;
30
31
+};
0 commit comments