Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 1c2bbab

Browse files
committed
918 finish
1 parent fa8634f commit 1c2bbab

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package problem0918
22

3+
// ref: https://leetcode.com/problems/maximum-sum-circular-subarray/discuss/178422/C++JavaPython-One-Pass
4+
35
func maxSubarraySumCircular(A []int) int {
4-
size := len(A)
5-
res := -30001
6-
tmp := 0
6+
total, curMax, curMin := 0, 0, 0
7+
maxSum, minSum := -30001, 30001
78

8-
for i := 0; i < size; i++ {
9-
for j := i + 1; j < i+size; j++ {
10-
if tmp < 0 {
11-
tmp=
12-
}
13-
}
9+
for _, a := range A {
10+
curMax = max(curMax+a, a)
11+
maxSum = max(maxSum, curMax)
12+
curMin = min(curMin+a, a)
13+
minSum = min(minSum, curMin)
14+
total += a
1415
}
15-
return 0
16+
17+
if maxSum > 0 {
18+
return max(maxSum, total-minSum)
19+
}
20+
return maxSum
1621
}
1722

1823
func max(a, b int) int {
@@ -21,3 +26,10 @@ func max(a, b int) int {
2126
}
2227
return b
2328
}
29+
30+
func min(a, b int) int {
31+
if a < b {
32+
return a
33+
}
34+
return b
35+
}

0 commit comments

Comments
 (0)