给定一个整数数组arrayofints
,找到三个整数的最高乘积Highestproduct
。输入的整数数组将始终至少有三个整数。
因此,我从arrayofints
中取出三个数字并将它们放入highestproduct
中:
Highestproduct = arrayofints[:2]
for item in arrayofints[3:]:
If min(Highestproduct) < item:
Highestproduct[highestproduct.index(min(Highestproduct))] = item
如果 highestproduct
中最小的数小于当前数: 用当前数替换最小的数。
这样做可以得到最高积,但显然有更好的解决方案。我的方法有什么问题吗?我的解决方案的时间复杂度是否为O(n)?