-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path628. Maximum Product of Three Numbers.py
46 lines (33 loc) · 1.16 KB
/
628. Maximum Product of Three Numbers.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
# Runtime: 272 ms, faster than 17.04% of Python3 online submissions for Maximum Product of Three Numbers.
# Memory Usage: 15.7 MB, less than 6.57% of Python3 online submissions for Maximum Product of Three Numbers.
class Solution:
def maximumProduct(self, nums: List[int]) -> int:
if len(nums) == 3:
return nums[0] * nums[1] * nums[2]
if max(nums) < 0:
nums = sorted(nums)
return nums[-1] * nums[-2] * nums[-3]
nums.sort()
nump = []
numn = []
for num in nums:
if num >= 0:
nump.append(num)
else:
numn.append(num)
if len(numn) >=2 and len(nump) >= 1:
answer1 = numn[0] * numn[1] * nump[-1]
if len(nump) >=3:
answer2 = nump[-1] * nump[-2] * nump[-3]
try:
return max(answer1,answer2)
except:
pass
try:
answer1
except NameError:
return answer2
try:
answer2
except NameError:
return answer1