Python solution with O(n) time and space O(1)


  • 0
    A
    class Solution(object):
        def maximumProduct(self, nums):
            length = len(nums)
            highestMax = -sys.maxint -1
            midMax = -sys.maxint -1
            lowMax = -sys.maxint -1
            highMin = sys.maxint
            lowMin = sys.maxint
            for i in nums:
                if i > highestMax:
                    lowMax = midMax
                    midMax = highestMax
                    highestMax = i
                elif i > midMax:
                    lowMax = midMax
                    midMax = i
                elif i > lowMax:
                    lowMax = i
                if i < lowMin:
                    highMin = lowMin
                    lowMin = i
                elif i < highMin:
                    highMin = i
            return max(highestMax*midMax*lowMax, highestMax*lowMin*highMin)
            """
            :type nums: List[int]
            :rtype: int
            """
            ```

Log in to reply
 

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