Given an array of ints, arrayofints
, find the highest product, Highestproduct
, you can get from three of the integers. The input array of ints will always have at least three integers.
So I've popped three numbers from arrayofints
and stuck them in highestproduct
:
Highestproduct = arrayofints[:2]
for item in arrayofints[3:]:
If min(Highestproduct) < item:
Highestproduct[highestproduct.index(min(Highestproduct))] = item
If min
of highestproduct
less than item: Replace the lowest number with the current number.
This would end up with highest product, but apparently there is a better solution. What's wrong with my approach? Would my solution be O(n)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…