forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
34 lines (33 loc) · 1.17 KB
/
main.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
class Solution:
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
return max(
nums[-1] * nums[-2] * nums[-3],
nums[-1] * nums[-2] * nums[0],
nums[-1] * nums[0] * nums[1],
nums[0] * nums[1] * nums[2],
)
if __name__ == '__main__':
import os, unittest, random
if os.getenv('LZS'): # local test
def brute(nums):
maxx = float('-inf')
for i, item1 in enumerate(nums):
for j, item2 in enumerate(nums):
for k, item3 in enumerate(nums):
if i not in (j, k) and j not in (i, k):
maxx = max(maxx, item1 * item2 * item3)
return maxx
class Test(unittest.TestCase):
def test_solution(self):
s = Solution()
for _ in range(10):
l = random.randint(3, 300)
print(l)
lst = [random.randint(-1000, 1000) for _ in range(l)]
self.assertEqual(brute(lst), s.maximumProduct(lst))
unittest.main()