Created
June 4, 2015 17:39
-
-
Save grapefroot/d103aa35a5521923f235 to your computer and use it in GitHub Desktop.
Max sum of contiguous subarray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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