Maximum Length of Subarray With Positive Product LeetCode solution in Python

1
class Solution:
2
def getMaxLen(self, arr: List[int]) -> int:
3
n = len(arr)
4

5
def solve(nums):
6
i, j, last_neg, neg, ans = 0, 0, None, 0, 0
7
while j < n:
8
while j < n and nums[j] != 0:
9
if nums[j] < 0:
10
neg += 1
11
last_neg = j
12
j += 1
13
if neg % 2 == 0:
14
ans = max(ans, j - i)
15
elif last_neg != None:
16
ans = max(ans, last_neg - i, j - last_neg - 1)
17
i, j, neg, last_neg = j + 1, j + 1, 0, None
18
return ans
19

20
return max(solve(arr), solve(arr[::-1]))

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0