-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2708-maximum-strength-of-a-group.py
More file actions
29 lines (25 loc) · 867 Bytes
/
2708-maximum-strength-of-a-group.py
File metadata and controls
29 lines (25 loc) · 867 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
# time complexity: O(n)
# space complexity: O(n)
from math import prod
from typing import List
class Solution:
def maxStrength(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
nums.sort()
negNums = [i for i in nums if i < 0]
posNums = [i for i in nums if i > 0]
if not negNums and not posNums:
return 0
if len(negNums) % 2 == 1:
negResult = prod(negNums[:-1]) if negNums[:-1] else 0
else:
negResult = prod(negNums)
posResult = prod(posNums) if posNums else 0
return max(negResult*posResult, posResult, negResult)
nums = [3, -1, -5, 2, 5, -9]
print(Solution().maxStrength(nums))
nums = [-4, -5, -4]
print(Solution().maxStrength(nums))
nums = [8, 6, 0, 5, -4, -8, -4, 9, -1, 6, -4, 8, -5]
print(Solution().maxStrength(nums))