Skip to content

Instantly share code, notes, and snippets.

@austintraver
Created October 7, 2021 02:26
Show Gist options
  • Save austintraver/6c4ff34053c6cf44a564ef12e96c0de0 to your computer and use it in GitHub Desktop.
Save austintraver/6c4ff34053c6cf44a564ef12e96c0de0 to your computer and use it in GitHub Desktop.
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
# Initialize our variables using the first element.
current_subarray = max_subarray = nums[0]
# Start with the 2nd element since we already used the first one.
for num in nums[1:]:
# If current_subarray is negative, throw it away. Otherwise, keep adding to it.
current_subarray = max(num, current_subarray + num)
max_subarray = max(max_subarray, current_subarray)
return max_subarray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment