File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ๊ณ๋จ ์ค๋ฅด๊ธฐ
3
+ * ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋:
4
+ * - ์๊ฐ๋ณต์ก๋: O(n) - ์
๋ ฅ๊ฐ ํฌ๊ธฐ์ ๋น๋กํ๋ ๋จ์ผ ๋ฐ๋ณต๋ฌธ
5
+ * - ๊ณต๊ฐ๋ณต์ก๋: O(1) - ์์ ๊ฐ์ ๋ณ์๋ง ์ฌ์ฉ
6
+ * @param n
7
+ */
8
+ function climbStairs ( n : number ) : number {
9
+ // 1 - 2 - 3 - 5 - 8 ... ๊ท์น ๋ฐ์
10
+ if ( n <= 3 ) return n
11
+
12
+ // ์ ๊ทผ (1) - ์๊ฐ๋ณต์ก๋๊ฐ ๋๋ฌด ํผ
13
+ // return climbStairs(n - 1) + climbStairs(n - 2) // ์๊ฐ
14
+
15
+ // ์ ๊ทผ (2)
16
+ // ํผ๋ณด๋์น ์์ด๊ณผ ๋น์ทํ ์์ ๋ ์ซ์๋ฅผ ๋ํด์ ๋ฐฐ์ด ๊ตฌ์กฐ๋ฅผ ๋ง๋ฌ
17
+ let current = 1 ; // ํ์ฌ ๋ฐฉ๋ฒ
18
+ let prev = 1 ; // ์ด์ ๋จ๊ณ์ ๋ฐฉ๋ฒ
19
+
20
+ // n-1๋ฒ ๋ฐ๋ณตํ์ฌ ๊ณ์ฐ
21
+ for ( let i = 1 ; i < n ; i ++ ) {
22
+ [ current , prev ] = [ current + prev , current ] ;
23
+ }
24
+
25
+ return current ;
26
+ }
You canโt perform that action at this time.
0 commit comments