File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * [Idea] - Dynamic programming
3
+ * ํ์ฌ ๊ณ๋จ์ ๋๋ฌํ๊ธฐ ์ํด์๋ 1์นธ ์ ๊ณ๋จ์์ 1์นธ ์ฌ๋ผ์ค๊ฑฐ๋ 2์นธ ์ ๊ณ๋จ์์ 2์นธ ์ฌ๋ผ์์ผ ํ๋ค.
4
+ * memo[i] = memo[i - 1] + memo[i - 2] ๋ก ๊ณ์ฐํ ์ ์๋ค.
5
+ *
6
+ * [Time Complexity]
7
+ * O(n) (n: ๊ณ๋จ์ ๊ฐ์)
8
+ * ๊ณ๋จ์ ๊ฐ์๋งํผ ๋ฐ๋ณต๋ฌธ์ ๋๋ค.
9
+ *
10
+ * [Space Complexity]
11
+ * O(n) (n: ๊ณ๋จ์ ๊ฐ์)
12
+ * memo ๋ฐฐ์ด์ n + 1 ํฌ๊ธฐ๋ก ์์ฑํ๋ค.
13
+ */
14
+ function climbStairs ( n : number ) : number {
15
+ const memo = new Array < number > ( n + 1 ) . fill ( 0 ) ;
16
+ memo [ 1 ] = 1 ;
17
+ memo [ 2 ] = 2 ;
18
+
19
+ for ( let currStep = 3 ; currStep <= n ; currStep ++ ) {
20
+ memo [ currStep ] = memo [ currStep - 1 ] + memo [ currStep - 2 ] ;
21
+ }
22
+
23
+ return memo [ n ] ;
24
+ }
You canโt perform that action at this time.
0 commit comments