-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighestPorductOfThree.py
More file actions
49 lines (31 loc) · 994 Bytes
/
HighestPorductOfThree.py
File metadata and controls
49 lines (31 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from itertools import islice
def HighestProductOf3(arr):
if len(arr) < 3:
raise IndexError("need at least three elements")
highest = max(arr[0], arr[1])
lowest = min(arr[0], arr[1])
highestProductOf2 = arr[0] * arr[1]
lowestProductOf2 = arr[0] * arr[1]
highestProductOf3 = arr[0] * arr[1] * arr[2]
for current in islice(arr, 2, None):
highestProductOf3 = max(
highestProductOf3,
current * highestProductOf2,
current * lowestProductOf2
)
highestProductOf2 = max(
highestProductOf2,
current * highest,
current * lowest
)
lowestProductOf2 = min(
highestProductOf2,
current * highest,
current * lowest
)
highest = max(highest, current)
lowest = min(lowest, current)
return highestProductOf3
arr = [1,2,3,4,5,6,15,16,17,1,2,3,4]
ans = HighestProductOf3(arr)
print(ans)