Skip to content

Instantly share code, notes, and snippets.

@mdpabel
Last active April 2, 2022 07:54
Show Gist options
  • Save mdpabel/4c54977b65187d17ead4940ef2d705d4 to your computer and use it in GitHub Desktop.
Save mdpabel/4c54977b65187d17ead4940ef2d705d4 to your computer and use it in GitHub Desktop.
def maxSubArray(self, nums: List[int]) -> int:
"""
-2,1,-3,4,-1,2,1,-5,4
l = -2
c = 1
l = 1
c = 1 - 3 = -2
c = 0
c = 4
l = 4
c = 4 - 1 = 3
c = 3 + 2 = 5
l = 5
c = 5 + 1
l = 6
c = 6 - 5 = 1
c = 1 + 5
"""
n = len(nums)
largest_sum = nums[0]
current_sum = largest_sum
for i in range(1, n):
if current_sum < 0:
current_sum = 0
current_sum += nums[i]
largest_sum = max(largest_sum, current_sum)
return largest_sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment