-
Notifications
You must be signed in to change notification settings - Fork 17
/
matrix-multiplication.go
66 lines (48 loc) · 1.19 KB
/
matrix-multiplication.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import "fmt"
import "math"
type Matrix struct {
n int
m int
}
func optimalParenthesesTopDown(matrices []Matrix) int {
dp := [][]int{}
for pos, _ := range matrices {
dp = append(dp, make([]int, len(matrices)))
for rowPos, _ := range dp[pos] {
dp[pos][rowPos] = math.MaxInt64 // assuming there is no int overflow
}
dp[pos][pos] = 0
}
for left := len(matrices) - 1; left >= 0; left-- {
for right := left; right < len(matrices); right++ {
for middle := left; middle < right; middle++ {
ops_left := dp[left][middle]
ops_right := dp[middle+1][right]
ops := ops_left + ops_right + matrices[left].n*matrices[middle].m*matrices[right].m
if ops < dp[left][right] {
dp[left][right] = ops
}
}
}
}
return dp[0][len(matrices)-1]
}
func getNumberOfMatrices() int {
var numMatrices int
fmt.Scanf("%d", &numMatrices)
return numMatrices
}
func getMatrix() Matrix {
var n, m int
fmt.Scanf("%d %d", &n, &m)
return Matrix{n, m}
}
func main() {
numMatrices := getNumberOfMatrices()
matrices := []Matrix{}
for pos := 0; pos < numMatrices; pos++ {
matrices = append(matrices, getMatrix())
}
fmt.Println(optimalParenthesesTopDown(matrices))
}