-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum_subarray.go
58 lines (52 loc) · 1009 Bytes
/
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
package main
import "math"
func maxSubArray(nums []int) int {
if len(nums) == 0 {
return 0
}
ans := nums[0]
curr := nums[0]
for i := 1; i < len(nums); i++ {
if curr > 0 {
curr += nums[i]
} else {
curr = nums[i]
}
ans = max(ans, curr)
}
return ans
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func maxSubArray2(nums []int) int {
return maxSubArray2Helper(nums, 0, len(nums)-1)
}
func maxSubArray2Helper(nums []int, l, r int) int {
if l == r {
return nums[l]
}
m := (l + r) / 2
lSum := maxSubArray2Helper(nums, l, m)
rSum := maxSubArray2Helper(nums, m+1, r)
mSUm := findMaxCrossingArray(nums, l, m, r)
return max(max(lSum, rSum), mSUm)
}
func findMaxCrossingArray(nums []int, l, m, r int) int {
lMaxSum := math.MinInt64
rMaxSum := math.MinInt64
sum := 0
for i := m; i >= l; i-- {
sum += nums[i]
lMaxSum = max(lMaxSum, sum)
}
sum = 0
for j := m + 1; j <= r; j++ {
sum += nums[j]
rMaxSum = max(rMaxSum, sum)
}
return lMaxSum + rMaxSum
}