Python 2.7: Solution O(n) time complexity and constant space complexity. All test passed!


#1
def maxProduct(self, A):
        minval = A[0]
        maxval = A[0]
        total = A[0]
        for i in range(1, len(A)):
            if A[i] < 0:
                minval, maxval = maxval, minval
            minval = min(A[i], minval*A[i])
            maxval = max(A[i], maxval*A[i])
            total = max(total, maxval)
        return total