Created
February 7, 2022 04:03
-
-
Save amarjitdhillon/152fc71128275b651743cf7822592a39 to your computer and use it in GitHub Desktop.
Maximum 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(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