File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ // ์ ์๊น์ง n๊ฑธ์์ด ์๋ชจ๋๋ค๊ณ ํ ๋, n์ ๊ตฌํ ์ ์๋ ์ค๋ณต๋์ง ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ์ฐํ๋ ๋ฌธ์
3+ // ๊ณ๋จ์ 1๊ฑธ์์ด๋ 2๊ฑธ์์ฉ ์ฌ๋ผ๊ฐ ์ ์๋ค.
4+ fun climbStairs (n : Int ): Int {
5+ // ๊ณ๋จ์ด 1๊ฐ๋ผ๋ฉด ๋ฌด์กฐ๊ฑด 1์ ๋ฐํ
6+ if (n == 1 ) return 1
7+ // ์ซ์๋ก ๋ ๋ฐฐ์ด์ ์ ์ธ(ํธ์์ 1๋ฒ์งธ ๊ณ๋จ๋ถํฐ ์์ํ๋๋ก n+1๋ก ์ ์ธ)
8+ val dp = IntArray (n + 1 )
9+ // ์ฒซ๋ฒ์งธ ๊ณ๋จ์ ์ฌ๋ผ๊ฐ๋ ๋ฐฉ๋ฒ์ 1๊ฐ์ง
10+ dp[1 ] = 1
11+ // ๋๋ฒ์งธ ๊ณ๋จ๊น์ง ์ฌ๋ผ๊ฐ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง(1+1, 2)
12+ dp[2 ] = 2
13+
14+ // ์ด์ 3๋ฒ์งธ ๊ณ๋จ๋ถํฐ n๋ฒ์งธ ๊ณ๋จ์ธ ์ ์๊น์ง ์ฌ๋ผ๊ฐ๋ ๋ฐฉ๋ฒ์ ๊ณ์ฐ
15+ for (i in 3 .. n){
16+ dp[i] = dp[i - 1 ] + dp[i - 2 ]
17+ }
18+ return dp[n]
19+ }
20+ }
21+
You canโt perform that action at this time.
0 commit comments