Skip to content

Instantly share code, notes, and snippets.

@grapefroot
Created June 4, 2015 17:39
Show Gist options
  • Save grapefroot/d103aa35a5521923f235 to your computer and use it in GitHub Desktop.
Save grapefroot/d103aa35a5521923f235 to your computer and use it in GitHub Desktop.
Max sum of contiguous subarray
#based on idea that none of the prefixes of
#maximal sum array can be negative(interesting, isn't it)
def maxSubArray(self, A):
cursum = 0
maxsum = -999999999999
for item in A:
cursum += item
maxsum = max(cursum, maxsum)
if cursum < 0:
cursum = 0
return maxsum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment