1567. Maximum Length of Subarray With Positive Product Solution 1 (time O(n), space O(1)) # Dynamic Programming class Solution(object): def getMaxLen(self, nums): """ :type nums: List[int] :rtype: int """ pos_length = 0 neg_length = 0 ans = 0 for num in nums: if num == 0: pos_length = 0 neg_length = 0 elif num > 0: pos_length += 1 if neg_length != 0: neg_length += 1 ans = max(ans, pos_length) else: cur_pos_length = pos_length cur_neg_length = neg_length if cur_neg_length != 0: pos_length = cur_neg_length + 1 else: pos_length = 0 if cur_pos_length != 0: neg_length = cur_pos_length + 1 else: neg_length = 1 ans = max(ans, pos_length) return ans