File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ //๊ณ๋จ ๊ฐฏ์ n ์
๋ ฅ๋ฐ์
3
+ func climbStairs( _ n: Int ) -> Int {
4
+ var result = 0
5
+
6
+ ///1๊ณ๋จ์ฉ ์ฌ๋ผ๊ฐ๋ ๊ฒฝ์ฐ = 1์คํญ
7
+ ///2๊ณ๋จ์ฉ ์ฌ๋ผ๊ฐ๋ ๊ฒฝ์ฐ = 2์คํญ
8
+ ///2์คํญ์ธ ๊ฒฝ์ฐ๋ฅผ 1์ฉ ์ฆ๊ฐ์์ผ์ค
9
+ for i in 0 ... ( n/ 2 ) {
10
+ ///2์คํญ์ด ์๋ ๊ฒฝ์ฐ -> ์ ๋ถ 1์คํญ์
11
+ if i == 0 {
12
+ result += 1
13
+ continue
14
+ }
15
+
16
+ ///n์์ 2์คํญ ํ์๋ฅผ ๋นผ์ 1์คํญ ํ์ ๊ตฌํ๊ธฐ
17
+ let x = n - ( 2 * i)
18
+
19
+ ///์กฐํฉ๊ณ์ฐ์ (2์คํญ,1์คํญ ์ ์ฒด ํ์ C 2์คํญ ํ์)
20
+ result += ncm ( x+ i, i)
21
+ }
22
+
23
+ return result
24
+ }
25
+
26
+ ///ncmํจ์๋ก ์กฐํฉ ๊ณ์ฐ์์ ๊ตฌํ
27
+ func ncm( _ n: Int , _ m: Int ) -> Int {
28
+ if m == 1 { return n } ///nC1 ์ด๋ผ๋ฉด ์ ์ฒดํ์ n ๋ฐํ
29
+ if m == n { return 1 } ///nCn ์ด๋ผ๋ฉด 1๋ฐํ
30
+
31
+ ///์กฐํฉ ๊ณ์ฐ์์ ์ฝ๋๋ก ๊ณ์ฐํ ์ ์๋๋ก ์ต์ ํํ๋ฉด ๋ค์๊ณผ ๊ฐ์์ง
32
+ return ( 1 ... m) . reduce ( 1 ) { $0 * ( $1 + n- m) / $1 }
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func climbStairs( _ n: Int ) -> Int {
3
+ var result = 0
4
+
5
+ for i in 0 ... ( n/ 2 ) {
6
+ if i == 0 {
7
+ result += 1
8
+ continue
9
+ }
10
+
11
+ let x = n - ( 2 * i)
12
+
13
+ result += ncm ( x+ i, i)
14
+ }
15
+
16
+ return result
17
+ }
18
+
19
+ func ncm( _ n: Int , _ m: Int ) -> Int {
20
+ if m == 1 { return n }
21
+ if m == n { return 1 }
22
+ return ( 1 ... m) . reduce ( 1 ) { $0 * ( $1 + n- m) / $1 }
23
+ }
24
+ }
You canโt perform that action at this time.
0 commit comments