Skip to content

Commit 65de835

Browse files
authored
Create 1567-maximum-length-of-subarray-with-positive-product.js
1 parent 324812e commit 65de835

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const getMaxLen = function(nums) {
6+
let res = 0, zeroIdx = -1, negIdx = -1, count = 0
7+
for(let i = 0, len = nums.length; i < len; i++) {
8+
if(nums[i] < 0) {
9+
count++
10+
if(negIdx === -1) negIdx = i
11+
}
12+
if(nums[i] === 0) {
13+
count = 0
14+
negIdx = -1
15+
zeroIdx = i
16+
} else {
17+
if(count % 2 === 0) res = Math.max(res, i - zeroIdx)
18+
else res = Math.max(res, i - negIdx)
19+
}
20+
}
21+
22+
return res
23+
};

0 commit comments

Comments
 (0)