Skip to content

Commit 08d18c9

Browse files
committedJun 26, 2022
Time: 897 ms (50.51%), Space: 28.4 MB (33.87%) - LeetHub
1 parent 4f428fe commit 08d18c9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
 

‎1567-maximum-length-of-subarray-with-positive-product/1567-maximum-length-of-subarray-with-positive-product.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
'''As we traverse through nums, we keep a running size/length of positive and negative products, represented by pos and neg, respectively
2+
3+
The logic is as follows:
4+
if current element is positive then
5+
-positive will be the previous positive length + 1 (because positive x positive = positive)
6+
-negative will be the previous negative length + 1 (because positive x negative = negative and only if a negative product length exists, otherwise its zero)
7+
if current element is negative then
8+
-positive will be the previous negative length + 1 (because negative x negative = positive but only if a neg exists, otherwise its zero)
9+
-negative will be the previous positive length + 1 (because positive x negative = negative)
10+
if current element is zero then
11+
-reset all lengths to zero
12+
13+
The questions is concerned with the maximum length subarray of positive products, so we only use this to update ans.'''
114
class Solution:
215
def getMaxLen(self, nums: List[int]) -> int:
316
n = len(nums)

0 commit comments

Comments
 (0)
Please sign in to comment.