Created
April 4, 2020 01:16
-
-
Save yesidays/14c6cfbff8e124e86fa8e4ce1ccf1d52 to your computer and use it in GitHub Desktop.
Maximum Subarray - Leetcode
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
# 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