Sharing my clear python code. Using DP.


  • 1
    M

    def maxProduct(self, A):

    maxp = [A[0]]
    minp = [A[0]]
    
    for i in xrange(1,len(A)):
        maxp.append(max(maxp[-1]*A[i],A[i],minp[-1]*A[i]))
        minp.append(min(maxp[-2]*A[i],A[i],minp[-1]*A[i]))
    
    return max(maxp+minp)
    

    Space cost can be O(1) if you like.


Log in to reply
 

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