-
Notifications
You must be signed in to change notification settings - Fork 0
/
918.maximum-sum-circular-subarray.go
106 lines (99 loc) · 1.85 KB
/
918.maximum-sum-circular-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
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
98
99
100
101
102
103
104
105
106
package solutions
import (
"math"
)
/*
* @lc app=leetcode id=918 lang=golang
*
* [918] Maximum Sum Circular Subarray
*/
/*
Your runtime beats 87.92 % of golang submissions
Your memory usage beats 69.08 % of golang submissions (7.3 MB)
*/
// @lc code=start
func maxSubarraySumCircular(nums []int) int {
totalSum := nums[0]
max := nums[0]
maxSum := nums[0]
min := nums[0]
minSum := nums[0]
for i := 1; i < len(nums); i++ {
totalSum += nums[i]
// find max sub array
newSum := maxSum + nums[i]
if newSum > nums[i] {
maxSum = newSum
} else {
maxSum = nums[i]
}
if maxSum > max {
max = maxSum
}
// find min sub array
newSum = minSum + nums[i]
if newSum < nums[i] {
minSum = newSum
} else {
minSum = nums[i]
}
if minSum < min {
min = minSum
}
}
if max < 0 {
return max
}
max2 := totalSum - min
// fmt.Printf("totalSum=%v, min=%v, max=%v, max2=%v\n", totalSum, min, max, max2)
if max2 > max {
return max2
}
return max
}
// @lc code=end
func maxSubarraySumCircular_timeout2(nums []int) int {
max := nums[0]
sum := nums[0]
sumIndex := 0
length := len(nums) * 2
for i := 1; i < length; i++ {
index := i % len(nums)
if i-sumIndex >= len(nums) {
sum -= nums[sumIndex]
sumIndex += 1
jSum := sum
for j := sumIndex; j < i; j++ {
jSum = jSum - nums[j%len(nums)]
if jSum > sum {
sum = jSum
sumIndex = j + 1
}
}
}
newSum := sum + nums[index]
if newSum > nums[index] {
sum = newSum
} else {
sum = nums[index]
sumIndex = i
}
if sum > max {
max = sum
}
}
return max
}
func maxSubarraySumCircular_timeout(nums []int) int {
sum := make([]int, len(nums))
max := math.MinInt32
for offset := 0; offset < len(nums); offset++ {
for i := 0; i < len(sum); i++ {
sum[i] += nums[(offset+i)%len(nums)]
if sum[i] > max {
max = sum[i]
}
}
}
return max
}