Skip to content

Instantly share code, notes, and snippets.

@yesidays
Created April 4, 2020 01:16
Show Gist options
  • Save yesidays/14c6cfbff8e124e86fa8e4ce1ccf1d52 to your computer and use it in GitHub Desktop.
Save yesidays/14c6cfbff8e124e86fa8e4ce1ccf1d52 to your computer and use it in GitHub Desktop.
Maximum Subarray - Leetcode
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3285/
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maximum = nums[0]
current = 0
for i in range(len(nums)):
current = max(nums[i], current + nums[i])
if current > maximum:
maximum = current
return maximum
def maxSubArrayBruteForce(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maximum = nums[0]
for i in range(len(nums) + 1):
for j in range(len(nums) + 1):
if nums[i:j]:
current = sum(nums[i:j])
if current > maximum:
maximum = current
return maximum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment