标签:空间复杂度 range inpu spl turn code split 空间 return
给定一个无序数组,包含正数、负数和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,0
for i in a:
if i<0:
if i<m1:
m2 = m1
m1 = i
elif i<m2:
m2 = i
elif i==0:
zero = True
else:
if i>p3:
p1 = p2
p2 = p3
p3 = i
elif i>p2:
p1 = p2
p2 = i
elif i>p1:
p1 = i
if 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())
标签:空间复杂度 range inpu spl turn code split 空间 return
原文地址:https://www.cnblogs.com/bernieloveslife/p/10504925.html