-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
binomialcoefficient.go
43 lines (36 loc) · 1.3 KB
/
binomialcoefficient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// binomialcoefficient.go
// description: Implementation of the binomial coefficient using dynamic programming
// details: The binomial coefficient C(n, k) is the number of ways to choose a subset of k elements from a set of n elements. The binomial coefficient is calculated using the formula C(n, k) = C(n-1, k-1) + C(n-1, k) with base cases C(n, 0) = C(n, n) = 1.
// time complexity: O(n*k) where n is the number of elements and k is the number of elements to choose
// space complexity: O(n*k) where n is the number of elements and k is the number of elements to choose
package dynamic
import "github.com/TheAlgorithms/Go/math/min"
// func main() {
// myArrayOfK := [4]int{5, 6, 7, 8}
// var x int
// fmt.Println("\nBinomial Coefficient Using Dynamic Programming:", bin2(50, 5))
// for _, element := range myArrayOfK {
// start := time.Now()
// x = bin2(50, element)
// elapsed := time.Since(start)
// fmt.Println("bin2 (50,", element, ") = ", x, " took ", elapsed)
// }
// }
// Bin2 function
func Bin2(n int, k int) int {
var i, j int
B := make([][]int, (n + 1))
for i := range B {
B[i] = make([]int, k+1)
}
for i = 0; i <= n; i++ {
for j = 0; j <= min.Int(i, k); j++ {
if j == 0 || j == i {
B[i][j] = 1
} else {
B[i][j] = B[i-1][j-1] + B[i-1][j]
}
}
}
return B[n][k]
}