-
Notifications
You must be signed in to change notification settings - Fork 0
/
linebreak.go
78 lines (62 loc) · 1.29 KB
/
linebreak.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
67
68
69
70
71
72
73
74
75
76
77
78
package linebreak
import "math"
type solver struct {
distMatrix [][]float64
targetDist float64
n int
memo []float64
parentPointers []int
}
func Solve(dists []float64, targetDist float64, f func(i, j int)) {
n := len(dists) + 1
memo := make([]float64, n)
distMatrix := make([][]float64, n)
for i := 0; i < n; i++ {
memo[i] = -1
distMatrix[i] = make([]float64, n)
}
for j := 1; j < n; j++ {
for i := 0; i < n-j; i++ {
distMatrix[i][i+j] = distMatrix[i][i+j-1] + dists[i+j-1]
}
}
s := solver{
distMatrix: distMatrix,
targetDist: targetDist,
n: n,
memo: memo,
parentPointers: make([]int, n),
}
s.solve(f)
}
func (s solver) solve(f func(i, j int)) {
s.dp(0)
var start, end int
for start != s.n-1 {
end = s.parentPointers[start]
f(start, end)
start = end
}
}
func (s solver) dp(i int) float64 {
if s.memo[i] >= 0 {
return s.memo[i]
}
if i == s.n-1 {
s.memo[i] = 0
return s.memo[i]
}
minBadness := math.Inf(1)
for j := i + 1; j < s.n; j++ {
badness := s.badness(i, j) + s.dp(j)
if badness < minBadness {
minBadness = badness
s.parentPointers[i] = j
}
}
s.memo[i] = minBadness
return minBadness
}
func (s solver) badness(i, j int) float64 {
return math.Pow(s.distMatrix[i][j]-s.targetDist, 2)
}