Skip to content

Instantly share code, notes, and snippets.

@amarjitdhillon
Created February 7, 2022 04:03
Show Gist options
  • Save amarjitdhillon/152fc71128275b651743cf7822592a39 to your computer and use it in GitHub Desktop.
Save amarjitdhillon/152fc71128275b651743cf7822592a39 to your computer and use it in GitHub Desktop.
Maximum Subarray
def maxSubArray(self, nums: List[int]) -> int:
current_sum, max_sum = 0, nums[0] # add first element to max array as len(nums) > 1 (given constaint)
for x in nums:
current_sum += x
if current_sum > max_sum: # update the max sum
max_sum = current_sum
# reset if sum is negative
if current_sum < 0:
current_sum = 0
return max_sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment