Created
October 7, 2021 02:26
-
-
Save austintraver/6c4ff34053c6cf44a564ef12e96c0de0 to your computer and use it in GitHub Desktop.
This file contains 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
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