Skip to content

Commit 4c7920d

Browse files
committed
Replaced Array2D with Swift inbuilt 2D array
1 parent 1a18101 commit 4c7920d

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Combinatorics/Combinatorics.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,22 @@ func quickBinomialCoefficient(n: Int, _ k: Int) -> Int {
100100
the calculation of much larger numbers, at the cost of temporary storage
101101
space for the cached values.
102102
*/
103+
103104
func binomialCoefficient(n: Int, _ k: Int) -> Int {
104-
var bc = Array2D(columns: n + 1, rows: n + 1, initialValue: 0)
105-
106-
for i in 0...n {
107-
bc[i, 0] = 1
108-
bc[i, i] = 1
109-
}
110-
111-
if n > 0 {
112-
for i in 1...n {
113-
for j in 1..<i {
114-
bc[i, j] = bc[i - 1, j - 1] + bc[i - 1, j]
115-
}
105+
var bc = Array(count: n + 1, repeatedValue: Array(count: n + 1, repeatedValue: 0))
106+
107+
for i in 0...n {
108+
bc[i][0] = 1
109+
bc[i][i] = 1
116110
}
117-
}
118-
119-
return bc[n, k]
111+
112+
if n > 0 {
113+
for i in 1...n {
114+
for j in 1..<i {
115+
bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]
116+
}
117+
}
118+
}
119+
120+
return bc[n][k]
120121
}

0 commit comments

Comments
 (0)