Skip to content

Commit dfaf030

Browse files
authored
Create 1567.Maximum-Length-of-Subarray-With-Positive-Product.cpp
1 parent abcb57e commit dfaf030

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int getMaxLen(vector<int>& nums)
4+
{
5+
int ret = 0;
6+
7+
for (int i=0; i<nums.size(); i++)
8+
{
9+
if (nums[i]==0) continue;
10+
int j = i;
11+
int count = 0;
12+
int firstNeg = -1;
13+
14+
while (j<nums.size() && nums[j]!=0)
15+
{
16+
count += (nums[j]<0);
17+
if (count % 2 ==0)
18+
ret = max(ret, j-i+1);
19+
else if (firstNeg!=-1)
20+
ret = max(ret, j-firstNeg);
21+
22+
if (firstNeg==-1 && nums[j]<0)
23+
firstNeg = j;
24+
j++;
25+
}
26+
i = j;
27+
}
28+
return ret;
29+
}
30+
};

0 commit comments

Comments
 (0)