-
Notifications
You must be signed in to change notification settings - Fork 1
/
4.1-maximum-subarray.go
71 lines (57 loc) · 1.54 KB
/
4.1-maximum-subarray.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
package main
import "fmt"
const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1
func main() {
A := []int{13, -3, -25, 20, 3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7}
B := []int{1, -4, 3, -4}
fmt.Println("B: ", B)
low, high, sum := findMaxSubarrayWrapper(B)
fmt.Println("Max subarray in B: ", low, high, sum)
fmt.Println("A: ", A)
low, high, sum = findMaxSubarrayWrapper(A)
fmt.Println("Max subarray in A: ", low, high, sum)
}
func findMaxCrossingSubarray(A []int, low, mid, high int) (int, int, int) {
leftSum := MinInt
sum := 0
maxLeft := 0
for i := mid; i >= low; i-- {
sum += A[i]
if sum > leftSum {
leftSum = sum
maxLeft = i
}
}
rightSum := MinInt
sum = 0
maxRight := 0
for j := mid + 1; j <= high; j++ {
sum += A[j]
if sum > rightSum {
rightSum = sum
maxRight = j
}
}
return maxLeft, maxRight, leftSum + rightSum
}
func findMaxSubarray(A []int, low, high int) (int, int, int) {
if high == low {
return low, high, A[low]
} else {
mid := (low + high) / 2
leftLow, leftHigh, leftSum := findMaxSubarray(A, low, mid)
rightLow, rightHigh, rightSum := findMaxSubarray(A, mid+1, high)
crossLow, crossHigh, crossSum := findMaxCrossingSubarray(A, low, mid, high)
if leftSum >= rightSum && leftSum >= crossSum {
return leftLow, leftHigh, leftSum
} else if rightSum >= leftSum && rightSum >= crossSum {
return rightLow, rightHigh, rightSum
} else {
return crossLow, crossHigh, crossSum
}
}
}
func findMaxSubarrayWrapper(A []int) (int, int, int) {
return findMaxSubarray(A, 0, len(A)-1)
}