53. Maximum Subarray
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 52% Topics: Array, Divide and Conquer, Dynamic Programming
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result, curr = float("-inf"), float("-inf")
for x in nums:
curr = max(curr+x, x)
result = max(result, curr)
return result
Solution from kamyu104/LeetCode-Solutions · MIT