aboutsummaryrefslogtreecommitdiff
path: root/max_len_subarr_pos_product.py
blob: 291ad6a45689a22de5da1a77dac5174a500d46f2 (plain)
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
# 1567. Maximum Length of Subarray With Positive Product

"""
given an array of integers 'nums', find the maximum length of subarray where
the product of all its elements is positive. a subarray of anarray is a
consecutive sequence of zero or more values taken out of that array. return
the maximum length of a subarray with positive product.
"""


class Solution(object):
    def getMaxLen(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n, pos, neg = len(nums), 0, 0
        if nums[0] > 0:
            pos = 1
        if nums[0] < 0:
            neg = 1
        ans = pos
        for i in range(1, n):
            if nums[i] > 0:
                pos += 1
                neg += 1 if neg > 0 else 0
            elif nums[i] < 0:
                pos, neg = 1 + neg if neg > 0 else 0, 1 + pos
            else:
                pos, neg = 0, 0
            ans = max(ans, pos)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.getMaxLen(nums=[1, -2, -3, 4]))
    print(obj.getMaxLen(nums=[0, 1, -2, -3, -4]))
    print(obj.getMaxLen(nums=[-1, -2, -3, 0, 1]))