-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum ProductofThreeNumbers.py
71 lines (61 loc) · 1.85 KB
/
Maximum ProductofThreeNumbers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
lis1 = []
lis2 = []
mul = 1
nums1 = [i for i in nums if i >= 0]
nums2 = [-1*i for i in nums if i < 0 ]
if(len(nums1) > 2):
if(len(nums2) < 2):
a = max(nums1)
index1 = nums1.index(a)
del nums1[index1]
b = max(nums1)
index2 = nums1.index(b)
del nums1[index2]
c = max(nums1)
#print("a")
else:
a = max(nums1)
index1 = nums1.index(a)
del nums1[index1]
b = max(nums1)
index2 = nums1.index(b)
del nums1[index2]
c = max(nums1)
d = max(nums2)
index4 = nums2.index(d)
del nums2[index4]
e = max(nums2)
lis1.extend([a,b,c])
lis2.extend([d,e])
if(b*c > d*e):
b = b
c = c
else:
b = d
c = e
#print("b")
elif(len(nums1)>=1 and len(nums1)<2):
a = nums1[0]
b = max(nums2)
index4 = nums2.index(b)
del nums2[index4]
c = max(nums2)
#print("c")
else:
a = max(nums2)
index1 = nums2.index(a)
del nums2[index1]
b = max(nums2)
index2 = nums2.index(b)
del nums2[index2]
c = max(nums2)
mul = mul*-1
#print("d")
mul = mul * a*b*c
return mul