Maximum Length of Subarray With Positive Product LeetCode solution in Java

1
class Solution {
2
public int getMaxLen(int[] nums) {
3
int first_negative = -1;
4
int zero_position = -1;
5
int count_neg = 0;
6
int res = 0;
7
for (int i = 0; i < nums.length; i++) {
8
if (nums[i] < 0) {
9
count_neg = count_neg + 1;
10
if (first_negative == -1) {
11
first_negative = i;
12
}
13
}
14
if (nums[i] == 0) {
15
count_neg = 0;
16
zero_position = i;
17
first_negative = -1;
18
} else {
19
if (count_neg % 2 == 0) {
20
res = Math.max(i - zero_position, res);
21
} else {
22
res = Math.max(i - first_negative, res);
23
}
24
}
25
}
26
return res;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0