DP, O(1)space, O(n)time


  • 0
    J
    class Solution(object):
        def maxProduct(self, nums):
            product=-sys.maxint-1; minp=maxp=0
            for i in nums:
                if i==0:
                    maxp=minp=0
                    product=max(product,0)
                elif minp==0:
                    minp=maxp=i
                    product=max(product,maxp)
                else:
                    minpi=minp*i
                    maxpi=maxp*i
                    minp=min(minpi,maxpi,i)
                    maxp=max(minpi,maxpi,i)
                    product=max(product,maxp,minp)   
            return product
    

Log in to reply
 

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