Python 5 lines O(n) time O(1) space, easy to understand


  • 3
    N
        def maxProduct(self, nums):
            neg, pos, maxp = 1, 1, -sys.maxsize-1
            for n in nums:
                neg, pos = (min(n, pos * n), neg * n) if n <= 0 else (neg * n, max(n, pos * n))
                maxp = max(maxp, pos)
            return maxp
    

  • 0
    P

    Very clean. I had started with Kadane's but couldn't resolve the negative elements.


Log in to reply
 

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