class Solution:
def getMaxLen(self, nums):
max_len = 0
start = 0
n = len(nums)
while start < n:
while start < n and nums[start] == 0:
start += 1
end = start
neg_count = 0
first_neg = -1
last_neg = -1
while end < n and nums[end] != 0:
if nums[end] < 0:
neg_count += 1
if first_neg == -1:
first_neg = end
last_neg = end
end += 1
if neg_count % 2 == 0:
max_len = max(max_len, end - start)
else:
if first_neg != -1:
max_len = max(max_len, end - first_neg - 1)
if last_neg != -1:
max_len = max(max_len, last_neg - start)
start = end + 1
return max_len
class Solution {
public:
int getMaxLen(vector<int>& nums) {
int maxLen = 0, n = nums.size();
int start = 0;
while (start < n) {
while (start < n && nums[start] == 0) start++;
int end = start, negCount = 0, firstNeg = -1, lastNeg = -1;
while (end < n && nums[end] != 0) {
if (nums[end] < 0) {
negCount++;
if (firstNeg == -1) firstNeg = end;
lastNeg = end;
}
end++;
}
if (negCount % 2 == 0) {
maxLen = max(maxLen, end - start);
} else {
if (firstNeg != -1)
maxLen = max(maxLen, end - firstNeg - 1);
if (lastNeg != -1)
maxLen = max(maxLen, lastNeg - start);
}
start = end + 1;
}
return maxLen;
}
};
class Solution {
public int getMaxLen(int[] nums) {
int maxLen = 0, n = nums.length;
int start = 0;
while (start < n) {
while (start < n && nums[start] == 0) start++;
int end = start, negCount = 0, firstNeg = -1, lastNeg = -1;
while (end < n && nums[end] != 0) {
if (nums[end] < 0) {
negCount++;
if (firstNeg == -1) firstNeg = end;
lastNeg = end;
}
end++;
}
if (negCount % 2 == 0) {
maxLen = Math.max(maxLen, end - start);
} else {
if (firstNeg != -1)
maxLen = Math.max(maxLen, end - firstNeg - 1);
if (lastNeg != -1)
maxLen = Math.max(maxLen, lastNeg - start);
}
start = end + 1;
}
return maxLen;
}
}
var getMaxLen = function(nums) {
let maxLen = 0;
let n = nums.length;
let start = 0;
while (start < n) {
while (start < n && nums[start] === 0) start++;
let end = start, negCount = 0, firstNeg = -1, lastNeg = -1;
while (end < n && nums[end] !== 0) {
if (nums[end] < 0) {
negCount++;
if (firstNeg === -1) firstNeg = end;
lastNeg = end;
}
end++;
}
if (negCount % 2 === 0) {
maxLen = Math.max(maxLen, end - start);
} else {
if (firstNeg !== -1)
maxLen = Math.max(maxLen, end - firstNeg - 1);
if (lastNeg !== -1)
maxLen = Math.max(maxLen, lastNeg - start);
}
start = end + 1;
}
return maxLen;
};
Given an array of integers nums
, find the length of the longest subarray where the product of all its elements is positive. A subarray is a contiguous part of the array. The product of an empty subarray is not considered. If there is no such subarray, return 0
.
Key constraints:
The brute-force way would be to check every possible subarray, compute its product, and keep track of the longest one with a positive product. However, this is very inefficient, especially for large arrays.
To optimize, notice that:
We solve the problem by breaking it into the following steps:
nums
, and for each segment (between zeros), process it separately.
Let's use the input nums = [1, -2, -3, 4]
.
Now, with nums = [0, 1, -2, -3, -4, 0, 5, 6, -7]
:
nums
, because it checks all subarrays.
The key insight is to break the array into segments separated by zeros and analyze each segment for the parity of negative numbers. By tracking the first and last negative, we can efficiently find the maximal positive product subarray in each segment. This approach avoids unnecessary computation and leverages properties of multiplication and sign, making the solution both elegant and efficient.