File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @description 1์นธ ๋๋ 2์นธ์ฉ ์ฌ๋ผ๊ฐ ์ ์์ ๋ n๋ฒ์งธ ๊ณ๋จ์ ๋๋ฌํ๋ ๊ฒฝ์ฐ์ ์
3+ * ์ ํ์: ways(n) = ways(n-1) + ways(n-2)
4+ * @param {number } n - ๊ณ๋จ์ ์ด ๊ฐ์
5+ * @returns {number } n๋ฒ์งธ ๊ณ๋จ๊น์ง ์ฌ๋ผ๊ฐ๋ ์๋ก ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์
6+ */
7+ function climbStairs ( n : number ) : number {
8+ // base case
9+ // n์ด 1์ด๋ฉด 1๊ฐ์ง
10+ // n์ด 2์ด๋ฉด (1+1, 2) ์ด 2๊ฐ์ง
11+ if ( n <= 2 ) return n ;
12+
13+ // a = ways(n-2)
14+ // b = ways(n-1)
15+ let a = 1 ;
16+ let b = 2 ;
17+
18+ // 3๋ฒ์งธ ๊ณ๋จ๋ถํฐ n๋ฒ์งธ ๊ณ๋จ๊น์ง ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ์ฐ
19+ for ( let i = 3 ; i <= n ; i ++ ) {
20+ // ํ์ฌ ๊ณ๋จ(i)์ ๋๋ฌํ๋ ๊ฒฝ์ฐ์ ์
21+ // = ๋ฐ๋ก ์ ๊ณ๋จ(i-1) + ๋ ์นธ ์ ๊ณ๋จ(i-2)
22+ const c = a + b ;
23+
24+ // ๋ค์ ๊ณ์ฐ์ ์ํด ๊ฐ ์ด๋
25+ // a โ ์ด์ ways(i-1)
26+ // b โ ํ์ฌ ways(i)
27+ a = b ;
28+ b = c ;
29+ }
30+
31+ return b ;
32+ }
You canโt perform that action at this time.
0 commit comments