Created
March 9, 2016 16:28
-
-
Save danielrobertson/f693337f1068ca02fec0 to your computer and use it in GitHub Desktop.
Maximum Contiguous Subarray
This file contains hidden or 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
| def maxSubarray(numbers): | |
| currentMax = 0 | |
| totalMax = 0 | |
| for n in numbers: | |
| currentMax = max(0, currentMax + n) | |
| totalMax = max(totalMax, currentMax) | |
| return totalMax | |
| numbers = [-10, 2,2,-4,2,3,4,-5] | |
| print(str(maxSubarray(numbers))) # 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment