线性扫描保存最大的三个和最小的两个数

  1. 均为正数时,最大的三个数相乘为最大
  2. 当含有两个最大负数时,再乘以最大正数为最大乘积
class Solution:
    def solve(self, A):
        # write code here
        max1 = max2 = max3 = float('-inf')
        min1 = min2 = float('inf')
        for n in A:
            if n >= max1:
                max2, max3 = max1, max2
                max1 = n
            elif n >= max2:
                max3, max2 = max2, n
            elif n > max3:
                max3 = n
            if n <= min1:
                min1, min2 = n, min1
            elif n < min2:
                min2 = n
        return max(min1 * min2 * max1, max1 * max2 * max3)