Skip to content

Commit 8a5aeb2

Browse files
authored
Create 0918-maximum-sum-circular-subarray.py
1 parent 663860a commit 8a5aeb2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Diff for: python/0918-maximum-sum-circular-subarray.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxSubarraySumCircular(self, nums: List[int]) -> int:
3+
globMax, globMin = nums[0], nums[0]
4+
curMax, curMin = 0, 0
5+
total = 0
6+
7+
for i, n in enumerate(nums):
8+
curMax = max(curMax + n, n)
9+
curMin = min(curMin + n, n)
10+
total += n
11+
globMax = max(curMax, globMax)
12+
globMin = min(curMin, globMin)
13+
14+
return max(globMax, total - globMin) if globMax > 0 else globMax

0 commit comments

Comments
 (0)