A simple O(n) python solution


  • 0
    M
    class Solution:
    # @param A, a list of integers
    # @return an integer
    def maxProduct(self, A):
        n = [A[0]]
        p = [A[0]]
        for i in range(1, len(A)):
            n.append(min([n[i - 1] * A[i], p[i - 1] * A[i], A[i]]))
            p.append(max([n[i - 1] * A[i], p[i - 1] * A[i], A[i]]))
        return max(p)

Log in to reply
 

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