[
dp
array
]
BinarySearch 0946 Maximum Length of Sublist with Positive Product
Problem statement
https://binarysearch.com/problems/Maximum-Length-of-Sublist-with-Positive-Product/
Solution
Equal to Leetcode 1567. Maximum Length of Subarray With Positive Product.
Complexity
It is O(n)
for time and O(1)
for space.
Code
class Solution:
def solve(self, A):
ans = pos = neg = 0
for x in A:
if x > 0:
pos, neg = pos + 1, neg + 1 if neg else 0
elif x < 0:
pos, neg = 1 + neg if neg else 0, 1 + pos
else:
pos = neg = 0
ans = max(ans, pos)
return ans