-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxsum.go
97 lines (82 loc) · 1.85 KB
/
maxsum.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package maxsum
import "math"
func MaxSum1(v []float64) (maxSofar float64) {
length := len(v)
for i := 0; i < length; i++ {
for j := i; j < length; j++ {
sum := 0.0
// sum of [i..j]
for k := i; k <= j; k++ {
sum += v[k]
}
maxSofar = math.Max(maxSofar, sum)
}
}
return
}
func MaxSum2(v []float64) (maxSofar float64) {
length := len(v)
for i := 0; i < length; i++ {
sum := 0.0
for j := i; j < length; j++ {
// sum of [i..j]
sum += v[j]
maxSofar = math.Max(maxSofar, sum)
}
}
return
}
func MaxSum2b(v []float64) (maxSofar float64) {
length := len(v)
// length+1 avoid sumArray[-1]
sumArray := make([]float64, length+1)
for i := 0; i < length; i++ {
sumArray[i+1] = sumArray[i] + v[i]
}
for i := 0; i < length; i++ {
for j := i; j < length; j++ {
maxSofar = math.Max(maxSofar, sumArray[j+1]-sumArray[i])
}
}
return
}
// ma, mb, mc
func maxSum(v []float64, low, high int) float64 {
if low > high { // zero elements
return 0
}
if low == high { // one element
return math.Max(0, v[low])
}
middle := (low + high) / 2
// find max crossing to left
lmax, sum := 0.0, 0.0
for i := middle; i >= low; i-- {
sum += v[i]
lmax = math.Max(lmax, sum)
}
// find max crossing to right
rmax, sum := 0.0, 0.0
for i := middle + 1; i <= high; i++ {
sum += v[i]
rmax = math.Max(rmax, sum)
}
mc := lmax + rmax
// recusively left && right
maxNow := math.Max(maxSum(v, low, middle), maxSum(v, middle+1, high))
maxNow = math.Max(maxNow, mc)
return maxNow
}
func MaxSum3(v []float64) (maxSofar float64) {
return maxSum(v, 0, len(v)-1)
}
// MaxSumSuite.BenchmarkMaxSum 20 96768717 ns/op
func MaxSum4(v []float64) (maxSofar float64) {
maxHere := 0.0
for length, i := len(v), 0; i < length; i++ {
maxHere = math.Max(maxHere+v[i], 0)
maxSofar = math.Max(maxSofar, maxHere)
}
return
}
var MaxSum = MaxSum4