给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大,要求时间复杂度:O(n),空间复杂度:O(1)
def solve():n = input()a = input().split()for i in range(len(a)):a[i] = int(a[i])m1,m2,p1,p2,p3 = 0,0,0,0,0for i in a:if i<0:if i<m1:m2 = m1m1 = ielif i<m2:m2 = ielif i==0:zero = Trueelse:if i>p3:p1 = p2p2 = p3p3 = ielif i>p2:p1 = p2p2 = ielif i>p1:p1 = iif p2==0 or p1==0:return m1*m2*p3# print(m1,m2,p1,p2,p3)return max(p1*p2*p3,m1*m2*p3)print(solve())