O(n) python solution

  • 0
    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def maxSubArray(self, nums):
            if nums:
                summer = nums[0]
                maximum = nums[0]
                for i in range(1, len(nums)):
                    summer = max(nums[i], summer + nums[i])
                    maximum = max(maximum, summer)
                return maximum
            else: return -1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.