File tree 2 files changed +51
-1
lines changed
2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,372 LeetCode solutions in JavaScript
1
+ # 1,373 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1196
1196
1562|[ Find Latest Group of Size M] ( ./solutions/1562-find-latest-group-of-size-m.js ) |Medium|
1197
1197
1563|[ Stone Game V] ( ./solutions/1563-stone-game-v.js ) |Hard|
1198
1198
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|
1199
1200
1576|[ Replace All ?'s to Avoid Consecutive Repeating Characters] ( ./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js ) |Medium|
1200
1201
1598|[ Crawler Log Folder] ( ./solutions/1598-crawler-log-folder.js ) |Easy|
1201
1202
1657|[ Determine if Two Strings Are Close] ( ./solutions/1657-determine-if-two-strings-are-close.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments