2708. Maximum Strength of a Group
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 25% Topics: Array, Dynamic Programming, Backtracking, Greedy, Bit Manipulation, Sorting, Enumeration
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# greedy
class Solution(object):
def maxStrength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if all(x <= 0 for x in nums) and sum(x < 0 for x in nums) <= 1:
return max(nums)
result = reduce(lambda x, y: x*y, (x for x in nums if x))
return result if result > 0 else result//max(x for x in nums if x < 0)
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions