-
Notifications
You must be signed in to change notification settings - Fork 0
/
069.py
51 lines (40 loc) · 1.28 KB
/
069.py
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
50
51
"""
Problem:
Given a list of integers, return the largest product that can be made by multiplying
any three integers.
For example, if the list is [-10, -10, 5, 2], we should return 500, since that's
-10 * -10 * 5.
You can assume the list has at least three integers.
"""
from typing import List
def largest_product_of_3(arr: List[int]) -> int:
# tracking the smallest 2 and the largest 3 numbers and generating the largest
# product using them
negative_1, negative_2 = 0, 0
positive_1, positive_2, positive_3 = 0, 0, 0
for elem in arr:
if elem < negative_1:
negative_2 = negative_1
negative_1 = elem
elif elem < negative_2:
negative_2 = elem
elif elem > positive_1:
positive_3 = positive_2
positive_2 = positive_1
positive_1 = elem
elif elem > positive_2:
positive_3 = positive_2
positive_2 = elem
elif elem > positive_3:
positive_3 = elem
return max(
positive_1 * negative_1 * negative_2, positive_1 * positive_2 * positive_3
)
if __name__ == "__main__":
print(largest_product_of_3([-10, -10, 5, 2]))
print(largest_product_of_3([10, -10, 5, 2]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""