Skip to content

Commit f19dca1

Browse files
committed
Add solution #1567
1 parent c7a265e commit f19dca1

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,372 LeetCode solutions in JavaScript
1+
# 1,373 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1196,6 +1196,7 @@
11961196
1562|[Find Latest Group of Size M](./solutions/1562-find-latest-group-of-size-m.js)|Medium|
11971197
1563|[Stone Game V](./solutions/1563-stone-game-v.js)|Hard|
11981198
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
1199+
1567|[Maximum Length of Subarray With Positive Product](./solutions/1567-maximum-length-of-subarray-with-positive-product.js)|Medium|
11991200
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
12001201
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12011202
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1567. Maximum Length of Subarray With Positive Product
3+
* https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers nums, find the maximum length of a subarray where the product of
7+
* all its elements is positive.
8+
*
9+
* A subarray of an array is a consecutive sequence of zero or more values taken out of that array.
10+
*
11+
* Return the maximum length of a subarray with positive product.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var getMaxLen = function(nums) {
19+
let result = 0;
20+
let positiveCount = 0;
21+
let negativeCount = 0;
22+
let firstNegativeIndex = -1;
23+
24+
for (let i = 0; i < nums.length; i++) {
25+
if (nums[i] === 0) {
26+
positiveCount = 0;
27+
negativeCount = 0;
28+
firstNegativeIndex = -1;
29+
continue;
30+
}
31+
32+
if (nums[i] > 0) {
33+
positiveCount++;
34+
} else {
35+
negativeCount++;
36+
if (firstNegativeIndex === -1) {
37+
firstNegativeIndex = i;
38+
}
39+
}
40+
41+
if (negativeCount % 2 === 0) {
42+
result = Math.max(result, positiveCount + negativeCount);
43+
} else {
44+
result = Math.max(result, i - firstNegativeIndex);
45+
}
46+
}
47+
48+
return result;
49+
};

0 commit comments

Comments
 (0)