1
# Runtime: 729 ms (Top 66.43%) | Memory: 18.9 MB (Top 76.46%)4
def kadanes(self, nums):11
for i in range(1, len(nums)):12
curr_max = max(curr_max + nums[i], nums[i])13
max_till_now = max(max_till_now, curr_max)17
def maxSubarraySumCircular(self, nums: List[int]) -> int:19
# there will be 2 case20
# case 1 : our max subarray is not wrapping i.e not circular21
# case 2: our max subarray is wrapping i.e circular23
# case 1 is easy to find24
# to find case 2 what we can do is if we multiply each nums element by -1 and25
# on that find kadanes then we will get sum of elements which is not part of maxsubarray in case2 (not part because we negate)26
# now subtract this newmax in case 2 from total nums sum, we get wrapping sum27
# max of case1 and case is our ans31
nonwrappingsum = self.kadanes(nums)33
# edge case when all elements are -ve then return max negative34
if nonwrappingsum < 0:38
for i in range(len(nums)):41
wrappingsum = total - (43
) # -ve because originally it was negated45
return max(nonwrappingsum, wrappingsum)