Python 1 line

  • 0
        def maxSubArray(self, nums):
            return reduce(lambda (g,l),x: (max(g,l+x),max(l+x,0)), nums, (None,0))[0]

    Translated from:

        def maxSubArray(self, nums):
            local_max, global_max = 0, None
            for num in nums:
                global_max = max(global_max, local_max+num)
                local_max = max(0, local_max+num)
            return global_max


Log in to reply

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