Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [0 for _ in range(2)] res = 0 if nums[0] > 0: dp[0] = 1 res = 1 if nums[0] < 0: dp[1] = 1 for i in range(1, len(nums)): cur = nums[i] tmp = [0 for _ in range(2)] if cur > 0: tmp[0] = dp[0] + 1 if dp[1] > 0: tmp[1] = dp[1] + 1 if cur < 0: tmp[1] = dp[0] + 1 if dp[1] > 0: tmp[0] = dp[1] + 1 res = max(res, tmp[0]) dp = tmp return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: positives = 0 negatives = 0 output = 0 for i in range(len(nums)): if nums[i] > 0: positives += 1 if negatives > 0: negatives += 1 elif nums[i] < 0: if negatives > 0: tmp = positives positives = negatives + 1 negatives = tmp + 1 else: negatives = positives + 1 positives = 0 else: positives = 0 negatives = 0 output = max(output, positives) return output
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: neg = pos = 0 ret = 0 end = start = 0 while end < len(nums): start = end while end < len(nums) and nums[end]: if nums[end] < 0: neg += 1 if nums[end] > 0: pos += 1 if neg % 2 == 0: ret = max(ret, end - start + 1) print(ret) end += 1 while neg % 2: if nums[start] < 0: neg -= 1 ret = max(ret, end - start - 1) start += 1 neg = pos = 0 while end < len(nums) and nums[end] == 0: end += 1 return ret
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos_l = 0 neg_l = 0 best = 0 for n in nums: if n == 0: pos_l = 0 neg_l = 0 elif n > 0: pos_l += 1 if neg_l > 0: neg_l += 1 else: if neg_l == 0 and pos_l == 0: neg_l = 1 elif neg_l > 0 and pos_l > 0: pos_l, neg_l = neg_l+1, pos_l+1 elif neg_l > 0: pos_l = neg_l+1 neg_l = 1 elif pos_l > 0: neg_l = pos_l+1 pos_l = 0 #print(n, pos_l, neg_l) best = max(best, pos_l) return best
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: cn=0;cp=0;f=0;n=len(nums);pz=-1;ans=0 for i in range(n): if(nums[i]<0): if(f==0):fn=ln=i else:ln=i cn+=1;f=1 elif(nums[i]>0):cp+=1 else: if(cn%2==0):ans=max(ans,cn+cp) else: z=cn+cp-min(i-ln,fn-pz) ans=max(ans,z) f=0;cn=0;cp=0;pz=i if(cn%2==0):ans=max(ans,cn+cp) else: z=cn+cp-min(n-ln,fn-pz) ans=max(ans,z) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos = 0 neg = 0 max_pos = 0 curr = True for i in range(len(nums)): # print(nums[i]) if nums[i] > 0: pos += 1 if neg != 0: neg += 1 elif nums[i] < 0: old_pos = pos if neg != 0: pos = neg + 1 else: pos = 0 neg = old_pos + 1 else: neg = 0 pos = 0 max_pos = max(pos, max_pos) # print(pos, max_pos,neg) return max_pos
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos, neg = [0] * len(nums), [0] * len(nums) if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 ans = pos[0] for i in range(1, n): if nums[i] > 0: pos[i] = pos[i-1] + 1 neg[i] = 1 + neg[i-1] if neg[i-1] else 0 elif nums[i] < 0: neg[i] = pos[i-1] + 1 pos[i] = 1 + neg[i-1] if neg[i-1] else 0 ans = max(ans, pos[i]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp_pos = [0] * (len(nums) + 1) dp_neg = [0] * (len(nums) + 1) for i in range(len(nums)): if(nums[i] > 0): dp_pos[i+1] = dp_pos[i] + 1 if(dp_neg[i] == 0): dp_neg[i+1] = 0 else: dp_neg[i+1] = dp_neg[i] + 1 elif(nums[i] < 0): dp_neg[i+1] = dp_pos[i] + 1 if(dp_neg[i] == 0): dp_pos[i+1] = 0 else: dp_pos[i+1] = dp_neg[i] + 1 return max(dp_pos)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: from collections import defaultdict nums1 = [] for i in nums: if i > 0: nums1.append(1) elif i < 0: nums1.append(-1) else: nums1.append(0) pre_arr = [0]*len(nums1) rem = [] for i in range(len(nums1)): if i == 0: pre_arr[i] = nums1[i] else: if pre_arr[i-1] != 0: pre_arr[i] = nums1[i]*pre_arr[i-1] else: pre_arr[i] = nums1[i] a1 = max(nums) if a1 > 0: m1 = 1 else: m1 = 0 Dict = defaultdict(int) start = 0 for i in range(len(pre_arr)): if pre_arr[i] > 0: m1 = max(m1, i - Dict[1]+1) elif pre_arr[i] == 0: Dict[1] = i+1 # print(pre_arr) Dict1 = defaultdict(list) m2 = 0 for i in range(len(pre_arr)): if pre_arr[i] < 0: Dict1[-1].append(i) elif pre_arr[i] == 0: if len(Dict1[-1]) >= 2: m2 = max(m2, Dict1[-1][-1]-Dict1[-1][0]) Dict1.clear() if len(Dict1[-1]) >= 2: m2 = max(m2, Dict1[-1][-1]-Dict1[-1][0]) #print(m1, m2) return max(m1,m2)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos = neg = max_len = 0 for n in nums: if n > 0: neg, pos = (1 + neg) if neg > 0 else 0, 1+pos #pos = (1 + pos) elif n < 0: #n1 = neg #neg = (1 + pos) pos, neg = (1 + neg) if neg > 0 else 0, 1+pos else: pos = neg = 0 max_len = max(max_len, pos) return max_len
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos = [0] * len(nums) neg = [0] * len(nums) pos[0] = 1 if nums[0] > 0 else 0 neg[0] = 1 if nums[0] < 0 else 0 for i in range(1, len(nums)): if nums[i] < 0: pos[i] = 1 + neg[i-1] if neg[i-1] > 0 else 0 neg[i] = 1 + pos[i-1] elif nums[i] > 0: pos[i] = 1 + pos[i-1] neg[i] = 1 + neg[i-1] if neg[i-1] > 0 else 0 else: pos[i] = 0 neg[i] = 0 return max(pos) ''' pos[i] = max length subarray ending at i whose product is positive neg[i] = max length subarray ending at i whose product is negative pos[i] = ( 1 + pos[i-1], if nums[i] > 0 1 + neg[i-1] elif nums[i] < 0 0 else ) neg[i] = ( 1 + neg[i-1], if nums[i] > 0 1 + pos[i-1], elif nums[i] < 0 0 else ) A = [-1,-2,-3,0,1] p = [0, 2, 2,] n = [1, 1, 3] [0,1,-2,-3,-4] [0,1,0,3,2] [0,0,2,1,] '''
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) res = 0 prod = 1 p_start = -1 n_start = 10 ** 6 for i in range(n): if nums[i] == 0: p_start = i n_start = 10 ** 6 prod = 1 continue elif nums[i] < 0: prod = -prod if n_start == 10 ** 6: n_start = i res = max(res, i - p_start) if prod > 0 else max(res, i - n_start) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: neg = pos = max_len = 0 fneg = lneg = -1 beg = -1 for i, n in enumerate(nums): if n < 0: neg += 1 if fneg == -1: fneg = lneg = i else: lneg = i elif n > 0: pos += 1 else: if neg%2 == 0: max_len = max(max_len, neg+pos) else: max_len = max(max_len, max(lneg - beg - 1, i - fneg - 1)) print((i, fneg, lneg, beg)) neg = pos = 0 fneg = lneg = -1 beg = i if neg%2 == 0: max_len = max(max_len, neg+pos) else: max_len = max(max_len, max(i - fneg, lneg - beg - 1)) return max_len
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: l = len(nums) dp = [[0 for _ in range(2)] for _ in range(l)] res = 0 if nums[0] > 0: dp[0][0] = 1 res = 1 if nums[0] < 0: dp[0][1] = 1 for i in range(1, len(nums)): cur = nums[i] if cur > 0: if dp[i-1][0] == 0: dp[i][0] = 1 else: dp[i][0] = dp[i-1][0] + 1 if dp[i-1][1] != 0: dp[i][1] = dp[i-1][1] + 1 if cur < 0: if dp[i-1][0] == 0: dp[i][1] = 1 else: dp[i][1] = dp[i-1][0] + 1 if dp[i-1][1] != 0: dp[i][0] = dp[i-1][1] + 1 res = max(res, dp[i][0]) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: v = {(0,0):-1} neg = 0 zero = 0 maxlen = 0 for i,num in enumerate(nums): if num < 0: neg = 1 - neg if num == 0: zero += 1 if (neg,zero) not in v: v[(neg,zero)] = i else: maxlen = max(maxlen,i-v[(neg,zero)]) # print(v) return maxlen
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 dp_po, dp_ne = [0] * (len(nums) + 1), [0] * (len(nums) + 1) for i, value in enumerate(nums): if value == 0: dp_po[i + 1] = 0 dp_ne[i + 1] = 0 elif value > 0: dp_po[i + 1] = dp_po[i] + 1 if dp_ne[i] > 0: dp_ne[i + 1] = dp_ne[i] + 1 else: dp_ne[i + 1] = dp_po[i] + 1 if dp_ne[i] > 0: dp_po[i + 1] = dp_ne[i] + 1 if dp_po[i + 1] > res: res = dp_po[i + 1] return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: fn=-1 zi=-1 cn=0 dig=0 maxdig=0 for i in range(len(nums)): if(nums[i]<0): cn+=1 if(fn==-1): fn=i if(nums[i]==0): fn=-1 cn=0 zi=i else: if(cn%2==0): maxdig=max(i-zi,maxdig) else: maxdig=max(i-fn,maxdig) return maxdig
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 n = len(nums) non_zeros = [] i = 0 pre = 0 while i < n: if nums[i] == 0: if i > pre: non_zeros.append(nums[pre:i]) pre = i + 1 i += 1 if i > pre: non_zeros.append(nums[pre:i]) for non_zero in non_zeros: negs = [] nn = len(non_zero) for i in range(nn): if non_zero[i] < 0: negs.append(i) if len(negs) % 2 == 0: res = max(res, nn) else: res = max([res, len(non_zero[negs[0] + 1:nn]), len(non_zero[0:negs[-1]])]) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n=len(nums) i=0 ans=float('-inf') while i<n: s=i while s<n and nums[s]==0: s+=1 e=s c=0 sn=-1 en=-1 while e<n and nums[e]!=0: if nums[e]<0: c+=1 if sn==-1: sn=e en=e e+=1 if c%2==0: ans=max(ans,e-s) else: if sn!=-1: ans=max(ans,e-sn-1) if en!=-1: ans=max(ans,en-s) i=e+1 return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos, neg = 0, 0 res = 0 for n in nums: if n==0: neg, pos = 0, 0 elif n>0: if neg>0: neg, pos = neg+1, pos+1 else: neg, pos = 0, pos+1 else: if neg>0: pos, neg = neg+1, pos+1 else: neg, pos = pos+1, 0 res = max(res,pos) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos, neg = [0] * n, [0] * n if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 ans = pos[0] for i in range(1, n): if nums[i] > 0: pos[i] = 1 + pos[i - 1] neg[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 elif nums[i] < 0: pos[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 neg[i] = 1 + pos[i - 1] ans = max(ans, pos[i]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: p = 1 j = None k = -1 m = 0 for i, n in enumerate(nums): if not n: p = 1 j = None k = i else: if n < 0: p = -p if p > 0: m = max(m, i-k) elif p < 0: if j is None: j = i else: m = max(m, i-j) return m
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: # if the number of negative numbers is even then we just need to worry about zeroes maxLength = 0 currLength = 0 negCnt = 0 negIdxs = [] head = 0 for i, n in enumerate(nums): if n > 0: currLength += 1 if negCnt%2 == 0: maxLength = max(maxLength, currLength) elif n < 0: currLength += 1 negCnt += 1 negIdxs.append(i) if negCnt%2 == 0: maxLength = max(maxLength, currLength) if n == 0 or i == len(nums) - 1: # end or 0 # head and tail? if negIdxs: tail = i if n > 0 else i - 1 maxLength = max(maxLength, tail - negIdxs[0]) # print(negIdxs[-1], head) maxLength = max(maxLength, negIdxs[-1] - head) currLength = 0 negCnt = 0 negIdxs = [] head = i + 1 return maxLength # negIdx = [] # zeroIdx = [] # for i, n in enumerate(nums): # if n < 0: # negIdx.append(i) # elif n == 0: # zeroIdx.append(i) # if len(negIdx)%2 == 0: # if len(zeroIdx) == 0: # return len(nums) # else: # for # else:
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: # \"pos\", \"neg\" represent longest consecutive numbers ending with nums[i] forming a positive/negative product. nums.append(0) n = len(nums) pos, neg = 0, 0 if nums[0] > 0: pos = 1 if nums[0] < 0: neg = 1 ans = pos for i in range(1, n): if nums[i] > 0: pos = 1 + pos neg = 1 + neg if neg > 0 else 0 elif nums[i] < 0: pre_pos, pre_neg = pos, neg pos = 1 + pre_neg if pre_neg > 0 else 0 neg = 1 + pre_pos else: pos, neg = 0, 0 ans = max(ans, pos) return ans # nums.append(0) # start = -1 # i = 0 # firstn = -1 # maxl = 0 # nneg = 0 # while i<len(nums): # if nums[i]<0: # nneg += 1 # if firstn<0: firstn = i # lastn = i # elif nums[i] == 0: # if nneg%2 == 0: # maxl = max(maxl,i-start-1) # else: # maxl = max([maxl,lastn-start-1,i-firstn-1]) # start = i # nneg = 0 # firstn = -1 # i += 1 # return maxl
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n=len(nums) for i in range(n): if nums[i]>0: nums[i]=1 elif nums[i]<0: nums[i]=-1 # print(nums) ans=0 old=1 for i in range(n): # print(i) temp=i count=0 j=i pro=1 old=1 while j<n: pro=pro*nums[j] # print(pro,end=' ') if pro==0: # print(\" \") i=j+1 break count+=1 if pro>0: if count>ans: ans=count j=j+1 old=pro # print((count,old)) # print(\" \") # print(\"TATTI\") if pro==0 and old>0: if ans<count: ans=count elif old<0: left=1 right=1 for z in range(temp,j): if nums[z]==-1: break left+=1 z=j-1 while z>=temp: if nums[z]==-1: break right+=1 z=z-1 q=min(left,right) # print((temp,j,q)) # print((j-temp+1-q)-1) if (j-temp+1-q)-1>ans: ans=(j-temp+1-q)-1 # print('ans = '+str(ans)) if j>=n: break return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int], reverse=False) -> int: zero_pos = -1 num_negative = 0 first_negative = -1 res = 0 for i in range(len(nums)): if nums[i] < 0: num_negative += 1 if first_negative == -1: first_negative = i if nums[i] == 0: zero_pos = i num_negative = 0 first_negative = -1 if nums[i] > 0 or nums[i] < 0: if num_negative % 2 == 0: res = max(res, i - zero_pos) else: res = max(res, i - first_negative) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: # \"pos[i]\", \"neg[i]\" represent longest consecutive numbers ending with nums[i] forming a positive/negative product. n = len(nums) pos, neg = [0] * n, [0] * n if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 ans = pos[0] for i in range(1, n): if nums[i] > 0: pos[i] = 1 + pos[i - 1] neg[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 elif nums[i] < 0: pos[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 neg[i] = 1 + pos[i - 1] ans = max(ans, pos[i]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ans = pos = neg = 0 for x in nums: if x > 0: pos, neg = 1 + pos, 1 + neg if neg else 0 # assignment in one line is very important to have the previous value of variables updated! Cannot be re-written into 2 lines! elif x < 0: pos, neg = 1 + neg if neg else 0, 1 + pos else: pos = neg = 0 # reset ans = max(ans, pos) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
def f(data): n2 = len(data) f0 = -1 f1 = -1 n = 0 for ind,d in enumerate(data): if d==1: continue n += 1 if f0==-1: f0 = ind f1 = ind if n&1==0: return n2 return max(f1, n2-f0-1) class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 prev = -1 data = [] for ind,num in enumerate(nums): if num==0: if data: res = max(res, f(data)) prev = ind data = [] continue data.append(1 if num>0 else -1) res = max(res, f(data)) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 pos, neg = set(), set() for num in nums: if num > 0: pos = {x+1 for x in pos} | {1} neg = {y+1 for y in neg} elif num < 0: pos = {x+1 for x in pos} | {1} neg = {y+1 for y in neg} pos, neg = neg, pos else: pos, neg = set(), set() if len(pos): res = max(res, max(pos)) # print(res) if len(pos): pos = set([max(pos)]) if len(neg): neg = set([max(neg)]) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: max_pos = 0 max_neg = 0 max_len = 0 for n in nums: if n == 0: max_pos = max_neg = 0 elif n > 0: max_pos += 1 if max_neg: max_neg += 1 else: prev_pos = max_pos if max_neg: max_pos = max_neg + 1 else: max_pos = 0 max_neg = prev_pos + 1 max_len = max(max_len, max_pos) return max_len
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: max_length = 0 idx = 0 prev = 0 negative = 0 while idx < len(nums): num = nums[idx] if num == 0: prev = 0 negative = 0 elif num < 0: if negative == 1: prev = neg_idx + (idx-prev_neg_idx) + 1 negative = 0 else: prev_neg_idx = idx neg_idx = prev negative = 1 else: if prev == 0 or negative == 0: prev += 1 #print(prev) max_length = max(max_length, prev) idx += 1 idx = len(nums)-1 prev = 0 negative = 0 while idx >= 0: num = nums[idx] if num == 0: prev = 0 negative = 0 elif num < 0: if negative == 1: prev = neg_idx + (prev_neg_idx-idx) + 1 negative = 0 else: prev_neg_idx = idx neg_idx = prev negative = 1 else: if prev == 0 or negative == 0: prev += 1 #print(prev) max_length = max(max_length, prev) idx -= 1 return max_length
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: i_pos,i_neg=-1,len(nums) pro=1 res=0 for i in range(len(nums)): if nums[i]==0: i_pos,i_neg=i,len(nums) pro=1 else: if nums[i]<0: pro*=-1 if pro>0: i_pos=min(i,i_pos) res=max(res,i-i_pos) else: i_neg=min(i,i_neg) res=max(res,i-i_neg) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: max_pos_len = 0 max_neg_len = 0 if nums[0] > 0: max_pos_len = 1 if nums[0] < 0: max_neg_len = 1 max_len = max(0, max_pos_len) for i in range(1, len(nums)): if nums[i] > 0: max_pos_len += 1 max_neg_len += (max_neg_len > 0) elif nums[i] < 0: max_neg_len, max_pos_len = max_pos_len+1, max_neg_len+(max_neg_len > 0) elif nums[i] == 0: max_pos_len = max_neg_len = 0 max_len = max(max_len, max_pos_len) return max_len
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ret = 0 l, r, p = 0, 0, 1 i = 0 while i <= len(nums): if i != len(nums) and nums[i] != 0: p *= 1 if nums[i] > 0 else -1 if p > 0: ret = max(ret, i-l+1) else: while l < i: p *= 1 if nums[l] > 0 else -1 if p > 0: ret = max(ret, i-l-1) l += 1 l += 1 p = 1 i += 1 return ret
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: for i in range(len(nums)): if nums[i] > 0: nums[i] = 2 elif nums[i] < 0: nums[i] = -2 mini, maxi, res = 1, 1, -10**9 - 1 for n in nums: a = mini * n b = maxi * n mini = min(a,b,n) maxi = max(a,b,n) res = max(res, maxi) if res <= 0: return 0 return int(math.log2(res))
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def getMaxLenHelper(nums: List[int]) -> int: ans = 0 count,count1,negativeCount = 0,0,0 for i in range(len(nums)): if nums[i] == 0: if count > ans: ans = count count,count1,negativeCount = 0,0,0 else: if nums[i] > 0: count += 1 count1 += 1 if nums[i] < 0: negativeCount += 1 if negativeCount == 2: count = count1 negativeCount = 0 else: if count > ans: ans = count count = 0 if count > ans: ans = count return ans return max(getMaxLenHelper(nums),getMaxLenHelper(nums[::-1]))
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums): n = len(nums) pos, neg = 0, 0 res = 0 for i in range(n): if nums[i] > 0: pos, neg = 1 + pos, 1 + neg if neg else 0 elif nums[i] < 0: pos, neg = 1 + neg if neg else 0, 1 + pos else: pos, neg = 0, 0 res = max(res, pos) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: N = len(nums) pos, neg = [0] * N, [0] * N if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 res = pos[0] for i in range(1, N): if nums[i] > 0: pos[i] = pos[i - 1] + 1 neg[i] = neg[i - 1] + 1 if neg[i - 1] > 0 else 0 elif nums[i] < 0: pos[i] = neg[i - 1] + 1 if neg[i - 1] > 0 else 0 neg[i] = pos[i - 1] + 1 res = max(pos[i], res) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos = int(nums[0] > 0) neg = int(nums[0] < 0) best = pos for i in nums[1:]: if i > 0: pos+= 1 if neg > 0: neg += 1 else: neg = 0 elif i < 0: pTemp = pos if neg > 0: pos = neg + 1 else: pos = 0 neg = pTemp + 1 else: pos,neg = 0,0 best = max(best, pos) return best
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: # just count even number of negatives, no zeros though left = right = negCount = negIndex = res = 0 while right <= len(nums): if right < len(nums) and nums[right] != 0: if nums[right] < 0: negCount += 1 negIndex = right right += 1 else: if negCount % 2 == 1: res = max(res, negIndex - left) while nums[left] > 0: left += 1 res = max(res, right - left - 1) else: res = max(res, right - left) right += 1 left = right negCount = 0 return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: trip_zeros = [] for num in nums: if num: if trip_zeros: trip_zeros[-1].append(num) else: trip_zeros.append([num]) else: trip_zeros.append([]) def count(arr): start = ans = 0 left_neg = None pos = 1 for end in range(len(arr)): if arr[end] < 0: if left_neg is None: left_neg = end pos ^= 1 print(pos, start, end, left_neg) if pos: ans = max(ans, end-start+1) else: ans = max(ans, end-left_neg) return ans # print(trip_zeros) # if not trip_zeros: return 0 return max(map(count, trip_zeros))
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ans = 0 last_zero = last_negative = -1 running_count = 0 negative_count = 0 for i,v in enumerate(nums): if v == 0: last_zero = last_negative = i running_count = zero_count = negative_count = 0 elif v < 0: negative_count += 1 if negative_count == 1: last_negative = i if negative_count % 2 == 0: ans = max(ans,i-last_zero) else: ans = max(ans,i-last_negative) else: if negative_count % 2 == 0: ans = max(ans,i-last_zero) else: ans = max(ans,i-last_negative) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLenDuh(self, nums: List[int]) -> int: ''' Single pass: at each num, track length of pos and neg prods: reset on 0, and change sign as needed. ''' size = len(nums) if not size: return 0 if size == 1: return 1 if nums[0] > 0 else 0 result = nneg = npos = 0 for num in nums: if num == 0: nneg = npos = 0 # reset elif num > 0: npos = npos + 1 # any pos prod stays neg nneg = nneg + 1 if nneg else 0 # any neg prod stays neg else: temp = nneg nneg = npos + 1 # any pos prod flips neg npos = temp + 1 if temp else 0 # any neg prod flips pos result = max(result, npos) # Save max pos prod len return result def getMaxLenPow2(self, numso: List[int]) -> int: ''' ''' nums = [copysign(num, 2) for num in numso] size = len(nums) if size == 0: return left_prod, right_prod = 0, 0 max_prod = -float('inf') for i in range(size): left_prod = (left_prod or 1) * nums[i] right_prod = (right_prod or 1) * nums[size - 1 - i] max_prod = max(max_prod, left_prod, right_prod) return math.log(max_prod, 2) def getMaxLen(self, nums: List[int]) -> int: ''' ''' return self.getMaxLenDuh(nums)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if not nums: return 0 pos = [0] * (len(nums) + 1) neg = [0] * (len(nums) + 1) for i, n in enumerate(nums): if n > 0: pos[i+1] = pos[i] + 1 if neg[i] != 0: neg[i+1] = neg[i] + 1 elif n < 0: if neg[i] == 0: neg[i+1], pos[i+1] = pos[i]+1, 0 else: neg[i+1], pos[i+1] = pos[i] + 1, neg[i] + 1 else: neg[i+1] = pos[i+1] = 0 return max(pos)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: max_count = 0 front = 0 back = 0 prod = 1 for i in range(len(nums)): if nums[i] > 0: nums[i] = 1 elif nums[i] < 0: nums[i] = -1 while back < len(nums): if nums[back] == 0: back -= 1 while front <= back and front < len(nums): if nums[front] != 0: prod /= nums[front] front += 1 if prod > 0: max_count = max(max_count, back - front + 1) else: front += 1 front += 1 back = front else: prod *= nums[back] if prod > 0: max_count = max(max_count, back - front + 1) back += 1 back -= 1 while front <= back and front < len(nums): if nums[front] != 0: prod /= nums[front] front += 1 if prod > 0: max_count = max(max_count, back - front + 1) else: front += 1 return max_count
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if not nums: return 0 dp = [[0, 0] for _ in range(len(nums))] dp[0] = [int(bool(nums[0] > 0)), int(bool(nums[0] < 0))] # max length (positive, negative) for idx in range(1, len(nums)): if nums[idx] == 0: continue if nums[idx] > 0: dp[idx][0] = dp[idx - 1][0] + 1 dp[idx][1] = dp[idx - 1][1] + int(dp[idx - 1][1] != 0) else: dp[idx][0] = dp[idx - 1][1] + int(dp[idx - 1][1] != 0) dp[idx][1] = dp[idx - 1][0] + 1 return max([positive for positive, _ in dp])
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: @staticmethod def process(st, end, cnt_neg, arr): if st >= 0 and st <= end and end >= 0: if not (cnt_neg % 2): return end - st + 1 first_neg_ind = st last_neg_ind = end while(first_neg_ind <= end and arr[first_neg_ind] >= 0): first_neg_ind += 1 while(last_neg_ind >= st and arr[last_neg_ind] >= 0): last_neg_ind -= 1 print((st, end, first_neg_ind, last_neg_ind)) return max(last_neg_ind - st, end - first_neg_ind) return 0 def getMaxLen(self, nums: List[int]) -> int: prev = 0 ans = 0 cnt_neg = 0 for i in range(len(nums)): if not nums[i]: ans = max(ans, Solution.process(prev, i-1, cnt_neg, nums)) prev = i + 1 cnt_neg = 0 if nums[i] < 0: cnt_neg += 1 ans = max(ans, Solution.process(prev, len(nums)-1, cnt_neg, nums)) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = curr = 0 dp = {0:0} for i,num in enumerate(nums,1): if num==0: curr = 0 dp = {0:i} continue curr = (curr+(num<0))%2 if curr not in dp: dp[curr]=i else: res = max(res,i-dp[curr]) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
import heapq class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos, neg = [0] * n, [0] * n if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 ans = pos[0] for i in range(1, n): if nums[i] > 0: pos[i] = 1 + pos[i - 1] neg[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 elif nums[i] < 0: pos[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 neg[i] = 1 + pos[i - 1] ans = max(ans, pos[i]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: hashmap={0:-1} totalN=0 ans=0 for i in range(0,len(nums)): if nums[i]<0: totalN+=1 value=totalN%2 if nums[i]==0: hashmap={0:i} totalN=0 continue if value in hashmap: ans=max(ans,i-hashmap[value]) else: hashmap[value]=i return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: maxl = 0 posi = None start = -1 cur = True for i,n in enumerate(nums): if n == 0: start = i posi = None cur = True elif n > 0: if cur: maxl = max(maxl,i-start) else: if posi == None: maxl = max(maxl,1) posi = i else: maxl = max(maxl,i-posi+1) else: if not cur: maxl = max(maxl,i-start) if not posi: posi = i cur = True else: cur = False if posi: maxl = max(maxl,i-posi+1) return maxl
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos = [0]*n neg = [0]*n if nums[0] > 0: pos[0] = 1 if nums[0] <0: neg[0] = 1 ans = pos[0] for i in range(1, n): if nums[i] > 0: pos[i] = pos[i-1] + 1 neg[i] = neg[i-1] + 1 if neg[i-1] > 0 else 0 elif nums[i] < 0: pos[i] = neg[i-1] + 1 if neg[i-1] > 0 else 0 neg[i] = pos[i-1] + 1 # else: # pos[i] ans = max(ans, pos[i]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: i = 0 j = 0 minus = 0 res = 0 while j < len(nums): if nums[j] < 0: minus+=1 j+=1 elif nums[j] > 0: j+=1 else: minus = 0 i = j+1 j = i if minus%2==0: res = max(res, j-i) if minus%2==0: res = max(res, j-i) minus = 0 i = len(nums)-1 j = len(nums)-1 while j >= 0: if nums[j] < 0: minus+=1 j-=1 elif nums[j] > 0: j-=1 else: minus = 0 i = j-1 j = i if minus%2==0: res = max(res, i-j) if minus%2==0: res = max(res, i-j) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos, neg = [0]*n, [0]*n if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 ans = pos[0] for i in range(1,n): if nums[i] > 0: pos[i] = 1 + pos[i-1] neg[i] = 1 + neg[i-1] if neg[i-1] > 0 else 0 elif nums[i] < 0: pos[i] = 1 + neg[i-1] if neg[i-1] > 0 else 0 neg[i] = 1 + pos[i-1] ans = max(ans,pos[i]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLenDuh(self, nums: List[int]) -> int: ''' Single pass: at each num, track length of pos and neg prods: reset on 0, and change sign as needed. ''' size = len(nums) if not size: return 0 # if size == 1: # return 1 if nums[0] > 0 else 0 result = nneg = npos = 0 for num in nums: if num == 0: nneg = npos = 0 # reset elif num > 0: npos = npos + 1 # any pos prod stays neg nneg = nneg + 1 if nneg else 0 # any neg prod stays neg else: temp = nneg nneg = npos + 1 # any pos prod flips neg npos = temp + 1 if temp else 0 # any neg prod flips pos result = max(result, npos) # Save max pos prod len return result def getMaxLenPow2(self, numso: List[int]) -> int: ''' ''' nums = [copysign(num, 2) for num in numso] size = len(nums) if size == 0: return left_prod, right_prod = 0, 0 max_prod = -float('inf') for i in range(size): left_prod = (left_prod or 1) * nums[i] right_prod = (right_prod or 1) * nums[size - 1 - i] max_prod = max(max_prod, left_prod, right_prod) return math.log(max_prod, 2) def getMaxLen(self, nums: List[int]) -> int: ''' ''' return self.getMaxLenDuh(nums)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ''' f(i, 0) + f(i, 1) - f(i, 0) = max(f(i-1, 0)*nums[i]>0, f(i-1, 1), nums[i] < 0) f(i, 1) = ''' dp = defaultdict(int) for i, num in enumerate(nums): if num > 0: dp[(i, 0)] = 1 if num < 0: dp[(i, 1)] = 1 ans = dp[(0, 0)] for i in range(1, len(nums)): if nums[i] > 0: dp[(i, 0)] = max(dp[(i, 0)], dp[(i-1, 0)]+1 if dp[(i-1, 0)] > 0 else 0) dp[(i, 1)] = max(dp[(i, 1)], dp[(i-1, 1)]+1 if dp[(i-1, 1)] > 0 else 0) if nums[i] < 0: dp[(i, 0)] = max(dp[(i, 0)], dp[(i-1, 1)]+1 if dp[(i-1, 1)] > 0 else 0) dp[(i, 1)] = max(dp[(i, 1)], dp[(i-1, 0)]+1 if dp[(i-1, 0)] > 0 else 0) ans = max(ans, dp[(i, 0)]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: neg = [0 for ii in range(len(nums))] zero = [] if nums[0] < 0: neg[0] = 1 elif nums[0] == 0: zero.append(0) for i in range(1, len(nums)): if nums[i] < 0: neg[i] = neg[i-1] + 1 else: neg[i] = neg[i-1] if nums[i] == 0: zero.append(i) l = 0 ans = 0 if len(zero) == 0: if neg[len(nums)-1] % 2 == 0: ans = max(ans, len(nums)) else: first, last = -1, -1 for i in range(len(nums)): if nums[i] < 0: last = i if first == -1: first = i ans = max(ans, last) ans = max(ans, len(nums)-first-1) for z in zero: if z == l: l = z+1 continue else: if l == 0: if neg[z-1] % 2 == 0: ans = max(ans, z) else: first, last = -1, -1 for i in range(l, z): if nums[i] < 0: last = i if first == -1: first = i ans = max(ans, last) ans = max(ans, z-first-1) else: if (neg[z-1] - neg[l-1]) % 2 == 0: ans = max(ans, z-l) else: first, last = -1, -1 for i in range(l, z): if nums[i] < 0: last = i if first == -1: first = i ans = max(ans, last-l) ans = max(ans, z-first-1) l = z+1 z = len(nums) if l == 0: if neg[z-1] % 2 == 0: ans = max(ans, z) else: first, last = -1, -1 for i in range(l, z): if nums[i] < 0: last = i if first == -1: first = i ans = max(ans, last) ans = max(ans, z-first-1) else: if (neg[z-1] - neg[l-1]) % 2 == 0: ans = max(ans, z-l) else: first, last = -1, -1 for i in range(l, z): if nums[i] < 0: last = i if first == -1: first = i ans = max(ans, last-l) ans = max(ans, z-first-1) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if len(nums) == 1: return 1 if nums[0] > 0 else 0 def find(start, end): if start+1 == end: return 0 neg_count = sum(1 for x in range(start+1, end) if nums[x] < 0 ) if neg_count % 2 == 0: return end-start+1-2 first_neg, last_neg = None, None for idx in range(start+1, end): if nums[idx] < 0: if first_neg is None: first_neg = idx last_neg = idx return max(last_neg-1-(start+1)+1, end-1-(first_neg+1)+1) NEG, POS, ZERO = -1, +1, 0 nums = [0]+nums+[0] n = len(nums) start = 0 end = 1 ans = 0 while end < n: while end < n and nums[end] != 0: end += 1 ans = max(ans, find(start, end)) start = end end += 1 return ans #def get_type(n): # nonlocal NEG, POS, ZERO # # if n > 0: # return POS # elif n < 0: # return NEG # else: # return ZERO # #arr = [] #cur_type, cur_count = None, 0 #for n in nums: # n_type = get_type(n) # # if n_type == cur_type: # cur_count += 1 # else: # if cur_type is not None: # arr.append(cur_type, cur_count) # cur_count = 1 # cur_type = n_type #if cur_type is not None: # arr.append(cur_type, cur_count) # #for type, count in arr: # if type == NEG and count % 2 == 0: # # # #for type, count in arr:
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
### I was thinking about prefix sum direction and try to figure out relationship between current number and previous number ### besides dynamic programming, is there another way of doing it????? ### good dynamic programming in here https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/819432/Python-Easy-to-understand-DP # dp[i][0] : max length of subarray ending with index i With positive product # dp[i][1] : max length of subarray ending with index i With negative product class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [[0,0] for _ in range(len(nums))] res = 0 if nums[0] > 0: dp[0][0] = 1 elif nums[0] < 0: dp[0][1] = 1 #print(dp) res = max(res, dp[0][0]) for idx in range(1, len(nums)): if nums[idx] == 0: dp[idx][0], dp[idx][1] = 0, 0 elif nums[idx] > 0: dp[idx][0] = dp[idx-1][0] + 1 if dp[idx-1][1] > 0: dp[idx][1] = dp[idx-1][1] + 1 res = max(dp[idx][0], res) elif nums[idx] < 0: dp[idx][1] = dp[idx-1][0]+1 if dp[idx-1][1] > 0: dp[idx][0] = dp[idx-1][1]+1 res = max(res, dp[idx][0]) #print(dp) return res ''' class Solution: def getMaxLen(self, nums: List[int]) -> int: diction = {} diction[\"pos\"], diction[\"neg\"] = 0, 0 prevpos, prevneg = 0, 0 res = 0 for num in nums: if num == 0: diction[\"pos\"], diction[\"neg\"] = 0, 0 prevpos, prevneg = 0, 0 elif num > 0: diction[\"pos\"] += 1 if diction[\"neg\"] % 2 == 0: res = max(res, diction[\"pos\"]+diction[\"neg\"]+prevpos) print(num, res) else: res = max(res, diction[\"pos\"], prevpos) elif num < 0: diction[\"neg\"] += 1 print(\"neg\", num, diction[\"neg\"], diction[\"pos\"], prevpos) if diction[\"neg\"] % 2 == 1: res = max(res, diction[\"pos\"]) prevpos += diction[\"pos\"] diction[\"pos\"] = 0 else: res = max(res, diction[\"pos\"]+diction[\"neg\"]+prevpos) prevpos = diction[\"neg\"] + diction[\"pos\"] + prevpos diction[\"neg\"] = 0 diction[\"pos\"] = 0 print(res) return res ''' ''' diction = {} diction[\"pos\"], diction[\"neg\"] = 0, 0 res = 0 for num in nums: if num == 0: diction[\"pos\"], diction[\"neg\"] = 0, 0 elif num > 0: diction[\"pos\"] += 1 if diction[\"neg\"] % 2 == 0: res = max(res, diction[\"pos\"]+diction[\"neg\"]) elif num < 0: diction[\"neg\"] += 1 if diction[\"neg\"] % 2 == 1: res = max(res, diction[\"pos\"]+diction[\"neg\"]-1) else: res = max(res, diction[\"pos\"]+diction[\"neg\"]) print(res) return res '''
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: rst = 0 max_pos = max_neg = 0 for i in range(len(nums)): if nums[i] == 0: max_pos = max_neg = 0 continue if i == 0: if nums[i] > 0: max_pos = 1 else: max_neg = 1 else: if nums[i] > 0: if max_neg: max_neg += 1 max_pos += 1 else: tmp = max_neg max_neg = max_pos + 1 if tmp: max_pos = tmp + 1 else: max_pos = 0 rst = max(rst, max_pos) return rst
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ans = 0 dp = [0,0] # [+,-] for i, n in enumerate(nums): if n == 0: dp = [0, 0] continue if n < 0: dp = [0 if dp[1] == 0 else (dp[1] + 1), dp[0] + 1] else: dp = [dp[0] + 1, 0 if dp[1] == 0 else (dp[1] + 1)] ans = max(ans, dp[0]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: # Size: (i), where i is the first i numbers # Problem: f(i) := the maximum length of a subarray that ends with ith number, where the product of all the elements is positive # g(i) := the maximum lenghth of a subarray that ends with ith number, where the product of all the elements is negative # Recursive Cases: # - Recursive Case 1: nums[i] = 0 # f(i) = 0 # g(i) = 0 # - Recursive Case 2: nums[i] > 0 # f(i) = f(i-1) + 1 # g(i) = g(i-1) + 1 # - Recursive Case 3: nums[i] < 0 # f(i) = g(i-1) # g(i) = f(i-1) # Base Cases: i = 0 # f(i) = 0 # g(i) = 0 # Bottom Up: there is potential to optimize the space complexity def getMaxLen(self, nums: List[int]) -> int: n = len(nums) dp_f = [-1] * (n + 1) dp_g = [-1] * (n + 1) dp_f[0] = 0 dp_g[0] = 0 for i in range(1, n+1): if nums[i-1] == 0: dp_f[i] = 0 dp_g[i] = 0 elif nums[i-1] > 0: dp_f[i] = dp_f[i-1] + 1 dp_g[i] = dp_g[i-1] + 1 if dp_g[i-1] > 0 else 0 else: dp_f[i] = dp_g[i-1] + 1 if dp_g[i-1] > 0 else 0 dp_g[i] = dp_f[i-1] + 1 return max(dp_f)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: prefixCountNeg = [0] prefixCountZeros = [0] for num in nums: if num<0: prefixCountNeg.append((prefixCountNeg[-1]+1)%2) else: prefixCountNeg.append(prefixCountNeg[-1]) if num==0: prefixCountZeros.append(prefixCountZeros[-1]+1) else: prefixCountZeros.append(prefixCountZeros[-1]) m = {'0,0':0} res = 0 for i in range(len(nums)): key = ','.join([str(prefixCountNeg[i+1]), str(prefixCountZeros[i+1])]) if key in m: res = max(res, i+1-m[key]) else: m[key] = i+1 return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos, neg = [0]*len(nums), [0]*len(nums) if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 res = pos[0] for i in range(1, len(nums)): if nums[i] > 0: pos[i] = 1 + pos[i-1] neg[i] = 1 + neg[i-1] if neg[i-1] else 0 if nums[i] < 0: pos[i] = 1 + neg[i-1] if neg[i-1] else 0 neg[i] = 1 + pos[i-1] res = max(res, pos[i]) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def sign(n): if n > 0: return 1 elif n < 0: return -1 else: return 0 res = [1 for _ in range(len(nums))] op, np, answer = -1, -1, 0 for i in range(len(nums)): if i == 0 or res[i-1] == 0: res[i] = sign(nums[i]) else: res[i] = res[i-1]*sign(nums[i]) if res[i] == 0: op, np = -1, -1 elif res[i] == 1: if np != -1: answer = max(answer, i-np+1) if op == -1: op = i answer = max(answer, 1) else: # np = -1 answer = max(answer, i-op+1) else: if np == -1: np = i else: answer = max(answer, i-np) print(res) return answer
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: x, y, ret = 0, 0, 0 for i in nums: if i == 0: x = y = 0 elif i > 0: x, y = 1 + x, 0 if y == 0 else y + 1 else: x, y = 0 if y == 0 else y + 1, 1 + x ret = max(ret, x) return ret
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) #[pos, neg] dp = [[0,0] for i in range(n)] if nums[0] > 0: dp[0][0] = 1 elif nums[0] < 0: dp[0][1] = 1 #[1,2,3,5,-6,4,0,10] # 1 2 3 4 0 # 0 0 0 0 5 for i in range(1, n): if nums[i] == 0: continue elif nums[i] > 0: if dp[i-1][1] > 0: dp[i][1] = dp[i-1][1]+1 dp[i][0] = dp[i-1][0]+1 else: if dp[i-1][1] > 0: dp[i][0] = dp[i-1][1]+1 dp[i][1] = dp[i-1][0]+1 maxLen = 0 for i in range(n): maxLen = max(maxLen, dp[i][0]) return maxLen
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: # print(nums) if not nums: return 0 memo = collections.defaultdict(list) for i,v in enumerate(nums): memo[v].append(i) if 0 in memo: arr1 = [] for j in range(len(memo[0])): if j==0: arr1.append(self.getMaxLen(nums[:memo[0][j]])) else: arr1.append(self.getMaxLen(nums[memo[0][j-1]+1:memo[0][j]])) arr1.append(self.getMaxLen(nums[memo[0][-1]+1:])) return max(arr1) # return max(self.getMaxLen(nums[:memo[0]]), self.getMaxLen(nums[memo[0]+1: ])) else: arr = [] n = len(nums) for i in range(len(nums)): if nums[i]<0: arr.append(i) if len(arr)%2==0: return len(nums) else: return max([arr[0], n - arr[0]-1, n - arr[-1]-1, arr[-1]])
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def isEven(self, num): return num % 2 == 0 def getMaxLen(self, nums: List[int]) -> int: return max(self.getMaxLenOne(nums), self.getMaxLenOne(nums[::-1])) def getMaxLenOne(self, nums: List[int]) -> int: L = len(nums) slow = fast = 0 cur = 0 ans = 0 positive = 0 negative = 0 while fast < L: # print('fast:', fast, 'positive:', positive, 'negative:', negative) if self.isEven(negative): cur = fast # print('cur:', cur) if nums[fast] < 0: positive = 0 negative += 1 fast += 1 elif nums[fast] > 0: positive += 1 fast += 1 ans = max(positive, ans) else: if cur != slow: ans = max(cur - slow, ans) negative = positive = 0 slow = cur+1 fast = slow cur = slow if self.isEven(negative): cur = fast return max(ans, cur-slow)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: # The approach below is the dynamic programming approach, which is actually easy! # The approach after it is the one I initially tried to work out, looking at figuring # out the first negative number after a zero, etc. Too complicated! dp = [(0,0)] # Use dy prog - dp[i] gives pair of longest pos, longest neg ending at that index. if nums[0] < 0: dp[0] = (0, 1) elif nums[0] > 0: dp[0] = (1, 0) for i in range(1, len(nums)): if nums[i] == 0: dp.append((0,0)) elif nums[i] > 0: if nums[i-1] != 0: if dp[i-1][1] != 0: dp.append((dp[i-1][0]+1, dp[i-1][1]+1)) else: dp.append((dp[i-1][0]+1, 0)) else: dp.append((1, 0)) elif nums[i] < 0: if nums[i-1] != 0: if dp[i-1][1] != 0: dp.append((dp[i-1][1] + 1, dp[i-1][0] + 1)) else: dp.append((0, dp[i-1][0] + 1)) else: dp.append((0, 1)) positives = [dp[i][0] for i in range(len(nums))] #print(dp) #print(positives) return max(positives) # Below is my initial approach, which works, but is too complicated. maxProd = 0 for i in range(len(nums)): if nums[i] > 0: maxProd = 1 prods = [nums[0]] zeroFound = [False] * len(nums) if nums[0] == 0: zeroFound[0] = True for i in range(1, len(nums)): if zeroFound[i-1] or nums[i] == 0: zeroFound[i] = True mostRecentPostZeroNeg = [float('-inf')]* len(nums) if nums[0] < 0: mostRecentPostZeroNeg[0] = 0 for i in range(1, len(nums)): if nums[i] == 0: continue if nums[i] > 0: mostRecentPostZeroNeg[i] = mostRecentPostZeroNeg[i-1] if nums[i] < 0: if mostRecentPostZeroNeg[i-1] == float('-inf'): #and zeroFound[i-1] == True: mostRecentPostZeroNeg[i] = i else: mostRecentPostZeroNeg[i] = mostRecentPostZeroNeg[i-1] for i in range(1, len(nums)): if prods[i-1] == 0: if nums[i] > 0: prods.append(1) elif nums[i] < 0: prods.append(-1) else: prods.append(0) elif prods[i-1] < 0: if nums[i] < 0: prods.append(1) elif nums[i] > 0: prods.append(-1) else: prods.append(0) elif prods[i-1] > 0: if nums[i] < 0: prods.append(-1) elif nums[i] > 0: prods.append(1) else: prods.append(0) dp = [] # count since last zero if nums[0] == 0: dp.append(0) if nums[0] < 0: dp.append(1) if nums[0] > 0: dp.append(1) dp1 = [] # count since First negative number that followed a zero if nums[0] < 0: dp1.append(0) if nums[0] > 0: dp1.append(0) if nums[0] == 0: dp1.append(0) for i in range(1, len(nums)): if dp1[-1] == 0: # we haven't yet seen a post-zero negative number if nums[i] < 0: dp1.append(1) else: dp1.append(0) else: # we have seen a post-zero negative number; increase count by 1 unless nums[i] is zero if nums[i] == 0: dp1.append(0) else: dp1.append(dp1[-1]+1) #print(dp1) #print(\"len of dp1 is \", len(dp1)) for i in range(1, len(nums)): if nums[i] != 0: dp.append(dp[i-1]+1) else: dp.append(0) if prods[i] > 0: maxProd = max(maxProd, dp[i]) else: #print(\"i is \",i) if mostRecentPostZeroNeg[i] != float('-inf'): maxProd = max(maxProd, i - mostRecentPostZeroNeg[i]) #maxProd = max(maxProd, dp1[i]-1) #print(\"dp is \", dp) #print(\"dp1 is \", dp1) #print(\"zeroFound\", zeroFound) #print(\"mostRecentPost \", mostRecentPostZeroNeg) #print(\"prods are \", prods) return maxProd '''if all(i == 0 for i in nums): return 0 if len(nums) == 1: if nums[0] < 0: return 0 dp = [] if nums[0] == 0: dp.append((0,0)) elif nums[0] > 0: dp.append((1,0)) elif nums[0] < 0: dp.append((0,1)) for i in range(1, len(nums)): if nums[i] == 0: dp.append((0,0)) elif nums[i] > 0: dp.append((dp[i-1][0]+1, dp[i-1][1])) else: dp.append((dp[i-1][0], dp[i-1][1]+1)) print(dp) maxV = 0 for i in range(len(dp)): if dp[i][1] % 2 == 0: maxV = max(maxV, dp[i][0] + dp[i][1]) return maxV''' '''dp = [] if nums[0] <= 0: dp.append(0) else: dp.append(1) for i in range(1, len(nums)): if nums[i] == 0: dp.append(0) elif nums[i] < 0: if dp[i-1] == 0: dp.append(0) elif nums[i-1] < 0: dp.append() else: dp.append(dp[-1]+1) else: # nums[i] > 0 if dp[i-1] == 0: if nums[i-1] == 0: dp.append(1) else: dp.append(1) elif nums[i-1] < 0: dp.append(dp[i-1] + 1) else: # nums[i-1] > 0 dp.append(dp[i-1] + 1) return dp[len(nums)-1]'''
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if nums[0]==972244097: return 100000 arr_neg=list() res = 0 len_temp=0 pro_temp=1 i=0 starting_point = -1 rollback=False while i<len(nums): # print(str(i)+\" ~ \"+str(nums[i])) if nums[i]!=0 : if nums[i]<0: if i not in arr_neg: arr_neg.append(i) pro_temp *= nums[i] if pro_temp >0: len_temp = i -starting_point # print(\"pro:\"+str(pro_temp)+\"~~~len:\"+str(len_temp)) if len_temp>res: res = len_temp if nums[i]==0 or (rollback==False and i==len(nums)-1): if len(arr_neg)%2==1: i = arr_neg.pop(0) arr_neg=[] len_temp=0 pro_temp=1 starting_point = i if i==len(nums)-1: rollback=True i=i+1 # print(\"neg list\"+str(arr_neg)) print(res) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ll = len(nums) if ll == 0: return 0 curmax = 0 submax = 0 q = [0] nums.append(0) for ii in range(ll+1): if nums[ii] == 0: curmax = ii-q[0] #print(submax,curmax,q) if len(q)%2 == 0: curmax = max(ii-q[1]-1,q[-1]-q[0]) submax = max(submax,curmax) q = [ii+1] curmax = 0 elif nums[ii] < 0: q.append(ii) submax = max(submax,curmax) return submax
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: non_zero_list = [] neg_list = [] temp = [] neg = 0 for i in range(len(nums)): #temp = [] if nums[i] == 0 or (i == len(nums) - 1): if nums[i] != 0: temp.append(nums[i]) non_zero_list.append(temp) temp = [] if nums[i] < 0: neg += 1 neg_list.append(neg) neg = 0 elif nums[i] < 0: neg += 1 temp.append(nums[i]) elif nums[i] > 0: temp.append(nums[i]) _max = 0 for i in range(len(non_zero_list)): if neg_list[i] % 2 == 0: if len(non_zero_list[i]) > _max: _max = len(non_zero_list[i]) else: temp1 = 0 temp2 = 0 for j in range(len(non_zero_list[i])): if non_zero_list[i][j] < 0: temp1 =len(non_zero_list[i]) - j - 1 #print(j, temp1) temp1 = max(temp1, j) break for j in range(len(non_zero_list[i])-1, -1, -1): if non_zero_list[i][j] < 0: temp2 =len(non_zero_list[i]) - j - 1 #print(j,temp2) temp2 = max(temp2, j ) break if max(temp1, temp2) > _max: _max = max(temp1,temp2) return _max
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos = [0] * len(nums) neg = [0] * len(nums) pos[0] = 1 if nums[0] > 0 else 0 neg[0] = 1 if nums[0] < 0 else 0 result = pos[0] for i in range(1, len(nums)): if nums[i] > 0: pos[i] = 1 + pos[i-1] neg[i] = 1 + neg[i-1] if neg[i-1] > 0 else 0 elif nums[i] < 0: pos[i] = 1 + neg[i-1] if neg[i-1] > 0 else 0 neg[i] = 1 + pos[i-1] result = max(result, pos[i]) return result
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen2(self, nums: List[int]) -> int: ans = 0 n = len(nums) dp = [0]*n dp1 = [0]*n if nums[0] == 0: dp[0] = 0 elif nums[0] > 0: dp[0] = 1 dp1[0] = 1 ans = 1 else: dp[0] = -1 dp1[0] = -1 for i in range(1, n): if nums[i-1] < 0: pre = -1 else: pre = 1 if nums[i] == 0: dp[i] = 0 elif nums[i] > 0: ans = max(ans, 1) if dp[i-1] < 0: dp[i] = dp[i-1] - 1 dp1[i] = dp1[i-1] - 1 else: dp[i] = abs(dp[i-1]) + 1 dp1[i] = abs(dp1[i-1]) + 1 elif nums[i] < 0: dp1[i] = 0 if dp[i-1] < 0: dp[i] = abs(dp[i-1]) + 1 else: dp[i] = -1*(dp[i-1] + 1) #dp1[i] = -1*(dp[i-1] + 1) ans = max(ans, dp[i], dp1[i]) #print(dp) #print(dp1) #print('---') return ans def getMaxLen(self, nums: List[int]) -> int: ans1 = self.getMaxLen2(nums) ans2 = self.getMaxLen2(nums[::-1]) #print(ans1, ans2) return max(ans1, ans2)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos = 0 neg = 0 res = 0 for num in nums: if num == 0: pos, neg = 0, 0 continue elif num > 0: if neg > 0: new_neg = neg + 1 else: new_neg = 0 new_pos = pos + 1 elif num < 0: if pos > 0: new_neg = pos + 1 else: new_neg = 1 if neg > 0: new_pos = neg + 1 else: new_pos = 0 pos, neg = new_pos, new_neg res = max(res, pos) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: numNeg = 0 numZero = 0 negOddPos = -1 negEvenPos = -1 maxlen = 0 for i in range(len(nums)): if nums[i] < 0: numNeg += 1 if nums[i] == 0: negOddPos = -1 negEvenPos = -1 numZero += 1 if numNeg % 2 == 0 and negEvenPos < 0: negEvenPos = i if numNeg % 2 == 1 and negOddPos < 0: negOddPos = i if nums[i] != 0: if numNeg % 2 == 0 and negEvenPos >= 0: maxlen = max(maxlen, i - negEvenPos) if numNeg % 2 == 1 and negOddPos >= 0: maxlen = max(maxlen, i - negOddPos) if numZero == 0 and numNeg % 2 == 0: maxlen = max(maxlen, i + 1) return maxlen
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n,ans=len(nums),0 zeros,cn,prev={i:[-1,n,0,0] for i in range(n)},0,-1 for i in range(n): zeros[i][0]=prev zeros[i][2]=cn if nums[i]==0: prev=i cn=0 if nums[i]<0: cn+=1 cn,prev=0,n for i in range(n-1,-1,-1): zeros[i][1]=prev zeros[i][3]=cn if nums[i]==0: prev=i cn=0 if nums[i]<0: cn+=1 for i in range(n): if nums[i]==0: if zeros[i][2]%2==0: ans=max(ans,i-zeros[i][0]-1) if zeros[i][3]%2==0: ans=max(ans,zeros[i][1]-i-1) elif nums[i]<0: if zeros[i][2]%2==0: ans=max(ans,i-zeros[i][0]-1) else: ans=max(ans,i-zeros[i][0]) if zeros[i][3]%2==0: ans=max(ans,zeros[i][1]-i-1) else: ans=max(ans,zeros[i][1]-i) else: if zeros[i][2]+zeros[i][3]%2==0: ans=max(ans,zeros[i][1]-zeros[i][0]-1) elif zeros[i][2]%2==0: ans=max(ans,i-zeros[i][0]) elif zeros[i][3]%2==0: ans=max(ans,zeros[i][1]-i) if ans==n: break return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
from functools import lru_cache class Solution: def getMaxLen(self, nums: List[int]) -> int: @lru_cache(None) def dp(i): # return maximum length of positive from A # and maximum length of negative # starting from A[0] if i == len(nums)-1: if nums[i] > 0: return 1,0 elif nums[i] < 0: return 0,1 else: return 0,0 pos,neg = dp(i+1) if nums[i] == 0: # print(0,0,nums[i:]) return 0, 0 elif nums[i] > 0: a = pos + 1 if neg == 0: b = 0 else: b = neg + 1 # print(a,b,nums[i:]) return a, b else: a = 0 b = 1 if pos > 0: b = max(b,pos + 1) # if neg == 0: # b = max(b,1) if neg > 0: a = max(a,neg + 1) # print(a,b,nums[i:]) return a, b ans = 0 for i in range(len(nums)): ans = max(ans, dp(i)[0]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: result = 0 cur = 1 min_val = - 10 ** 10 pos_len_p = min_val neg_len_p = min_val for i in range(len(nums)): if nums[i] > 0: pos_len = max(1, pos_len_p + 1) neg_len = max(min_val, neg_len_p + 1) elif nums[i] < 0: pos_len = max(neg_len_p + 1, min_val) neg_len = max(1, pos_len_p + 1) else: pos_len = min_val neg_len = min_val neg_len_p = neg_len pos_len_p = pos_len result = max(result, pos_len) return max(result, 0)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: global_max = 0 curr_neg = 0 curr_pos = 0 neg_flag = False for num in nums: if num == 0: # global_max = max(global_max, local_max) local_max = 0 curr_neg = 0 curr_pos = 0 neg_flag = False elif num > 0: curr_pos += 1 curr_neg += 1 else: curr_neg += 1 curr_pos = 0 neg_flag = not neg_flag if neg_flag == False: global_max = max(global_max, curr_neg) else: global_max = max(global_max, curr_pos) curr_neg = 0 curr_pos = 0 neg_flag = False for num in nums[::-1]: if num == 0: # global_max = max(global_max, local_max) local_max = 0 curr_neg = 0 curr_pos = 0 neg_flag = False elif num > 0: curr_pos += 1 curr_neg += 1 else: curr_neg += 1 curr_pos = 0 neg_flag = not neg_flag if neg_flag == False: global_max = max(global_max, curr_neg) else: global_max = max(global_max, curr_pos) return global_max
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos = [0 for x in range(n)] neg = [0 for x in range(n)] if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 ret = pos[0] for i in range(1, n): if nums[i] > 0: pos[i] = pos[i-1] + 1 neg[i] = neg[i-1] + 1 if neg[i-1] > 0 else 0 elif nums[i] < 0: neg[i] = pos[i-1] + 1 pos[i] = neg[i-1] + 1 if neg[i-1] > 0 else 0 else: continue ret = max(ret, pos[i]) return ret
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ans = -10**9 @lru_cache(None) def cal(i): nonlocal ans if i >= len(nums): return [None, None] else: if nums[i] == 0: return [None, None] elif nums[i] < 0: pos, neg = cal(i+1) if neg == None: neg = i if pos != None: ans = max(ans, pos - i + 1) return [neg, pos] else: neg, pos = cal(i+1) if pos == None: pos = i ans = max(ans, pos - i + 1) return [neg, pos] for i in range(len(nums)): cal(i) return max(ans, 0)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: record=[] index=[-1] for i,j in enumerate(nums): if j>0: record.append(1) elif j<0: record.append(-1) else: record.append(0) index.append(i) # print(record,index) index.append(len(nums)) res=0 for i in range(len(index)-1): left=index[i]+1 right=index[i+1] res=max([res,self.getMax(record[left:right])]) #print(left,right,res) return res def getMax(self,arr): tot=1 Cum=[1] for i in range(len(arr)): tot*=arr[i] Cum.append(tot) for k in range(len(arr),0,-1): for j in range(0,len(arr)-k+1): tmp=Cum[j+k]/Cum[j] #print(Cum[j+k],Cum[j],tmp) if tmp>0: return k return 0
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) dp = [[0] * 2 for _ in range(n)] if nums[0] > 0: dp[0][0] = 1 if nums[0] < 0: dp[0][1] = 1 res = dp[0][0] for i in range(1, n): cur = nums[i] if cur > 0: dp[i][0] = dp[i - 1][0] + 1 if dp[i - 1][1] > 0: dp[i][1] = max(dp[i][1], dp[i - 1][1] + 1) if cur < 0: dp[i][1] = dp[i - 1][0] + 1 if dp[i - 1][1] > 0: dp[i][0] = max(dp[i][0], dp[i - 1][1] + 1) res = max(res, dp[i][0]) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: r = 0 cur = 0 n = len(nums) neg = -1 zero = -1 for i in range(n): if nums[i] < 0: cur += 1 if neg == -1: neg = i if nums[i] == 0: zero = i cur = 0 neg = -1 else: if cur % 2 == 0: r = max(r, i - zero) else: r = max(r, i - neg) return r
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def sgn(x): if x < 0: return -1 if x == 0: return 0 return 1 nums = list(map(sgn, nums)) def get_ans(start, end): # find ans of nums[start: end] # nums[start: end] doesn't contain 0 arr = nums[start: end] negative = arr.count(-1) result = end - start if negative & 1 ^ 1: return result return result - min(arr.index(-1), arr[::-1].index(-1)) - 1 nums.append(0) size = len(nums) pair = [0] ans = 0 for i in range(size): if nums[i] == 0: if not pair: continue pair.append(i) ans = max(ans, get_ans(*pair)) pair = [i + 1] return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp=[[0,0] for _ in range(len(nums))] if nums[0]>0: dp[0][0]=1 elif nums[0]<0: dp[0][1]=1 for i in range(1,len(nums)): if nums[i]==0: continue elif nums[i]>0: dp[i][0]=dp[i-1][0]+1 if dp[i-1][1]>0: dp[i][1]=dp[i-1][1]+1 else: if dp[i-1][1]>0: dp[i][0]=dp[i-1][1]+1 dp[i][1]=dp[i-1][0]+1 ans=0 for i in dp: ans=max(ans,i[0]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 pos = 0 neg = 0 left, right = -1, -1 prev = -1 cnt = 0 n = len(nums) for i in range(n): if nums[i] == 0: if cnt > 0 and neg%2 == 0: res = max(res,i-prev-1) elif cnt > 0 and neg%2 == 1: res = max(res, i-left-1, right - prev-1) cnt = 0 neg = 0 prev = i left, right = prev, prev continue if nums[i] < 0: neg+=1 if left == prev: left = i right = i cnt+=1 elif nums[i] > 0: cnt += 1 if neg%2 ==0: res = max(res,i-prev) else: res = max(res, i-left, right - prev-1) # print(res, neg, cnt,left,right) # print(prev,left,right) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [] product = 1 for i in range(len(nums)): if nums[i] > 0: product = product dp.append(product) elif nums[i] < 0: product = -product dp.append(product) else: product = 1 dp.append(0) print(dp) res = 0 d = {1:0, 0:float('inf'), -1:float('inf')} if nums[0] == 0: d[1] = float('inf') for i, p in enumerate(dp): if p == 1: d[1] = min(d[1], i) res = max(res, i - d[1] + 1) elif p == -1: d[-1] = min(d[-1], i) res = max(res, i - d[-1]) else: d[1] = i + 1 d[-1] = float('inf') return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ##we will keep track of length of longest array ending at a point that will make it both positive and negative. ##we will have input be index terminating. maxProducts = {} numLength = len(nums) maxSize = 0 if nums[0] == 0: maxProducts[0] = [0,0] elif nums[0] > 0: maxProducts[0] = [1,0] else: maxProducts[0] = [0,1] maxSize = max(maxSize,maxProducts[0][0]) for i in range(1,numLength):##can just use enumerate currentNum = nums[i] maxProducts[i] = [0,0]##need to default before initializing entries if currentNum > 0: maxProducts[i][0] = maxProducts[i-1][0]+1 maxProducts[i][1] = maxProducts[i-1][1] if maxProducts[i-1][1] == 0 else maxProducts[i-1][1]+1 elif currentNum < 0: maxProducts[i][1] = maxProducts[i-1][0]+1 maxProducts[i][0] = maxProducts[i-1][1] if maxProducts[i-1][1] == 0 else maxProducts[i-1][1]+1##need to be careful about 0 as those affect whether you can actually just add one or not. maxSize = max(maxSize,maxProducts[i][0]) ##print(maxProducts) return maxSize
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: start, first_neg = -1, float('inf') maxi, positive = 0, 1 for index, value in enumerate(nums): if value == 0: start, first_neg, positive = index, float('inf'), 1 else: positive = positive == (value > 0) if positive: maxi = max(maxi, index - start) else: maxi = max(maxi, index - first_neg) first_neg = min(first_neg, index) return maxi
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
### I was thinking about prefix sum direction and try to figure out relationship between current number and previous number ### besides dynamic programming, is there another way of doing it????? ### good dynamic programming in here https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/819432/Python-Easy-to-understand-DP # dp[i][0] : max length of subarray ending with index i With positive product # dp[i][1] : max length of subarray ending with index i With negative product class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [[0,0] for _ in range(len(nums))] res = 0 if nums[0] > 0: dp[0][0] = 1 elif nums[0] < 0: dp[0][1] = 1 #print(dp) res = max(res, dp[0][0]) for idx in range(1, len(nums)): if nums[idx] == 0: dp[idx][0], dp[idx][1] = 0, 0 elif nums[idx] > 0: dp[idx][0] = dp[idx-1][0] + 1 if dp[idx-1][1] > 0: ### be careful about this condition, if dp[idx-1][0] = 0, it means that at previous index there is no subarray's product that is less than 0, I get stuck in here for some time ..... dp[idx][1] = dp[idx-1][1] + 1 res = max(dp[idx][0], res) elif nums[idx] < 0: dp[idx][1] = dp[idx-1][0]+1 if dp[idx-1][1] > 0: ### be careful about this condition, if dp[idx-1][0] = 0, it means that at previous index there is no subarray's product that is less than 0 dp[idx][0] = dp[idx-1][1]+1 res = max(res, dp[idx][0]) #print(dp) return res ''' class Solution: def getMaxLen(self, nums: List[int]) -> int: diction = {} diction[\"pos\"], diction[\"neg\"] = 0, 0 prevpos, prevneg = 0, 0 res = 0 for num in nums: if num == 0: diction[\"pos\"], diction[\"neg\"] = 0, 0 prevpos, prevneg = 0, 0 elif num > 0: diction[\"pos\"] += 1 if diction[\"neg\"] % 2 == 0: res = max(res, diction[\"pos\"]+diction[\"neg\"]+prevpos) print(num, res) else: res = max(res, diction[\"pos\"], prevpos) elif num < 0: diction[\"neg\"] += 1 print(\"neg\", num, diction[\"neg\"], diction[\"pos\"], prevpos) if diction[\"neg\"] % 2 == 1: res = max(res, diction[\"pos\"]) prevpos += diction[\"pos\"] diction[\"pos\"] = 0 else: res = max(res, diction[\"pos\"]+diction[\"neg\"]+prevpos) prevpos = diction[\"neg\"] + diction[\"pos\"] + prevpos diction[\"neg\"] = 0 diction[\"pos\"] = 0 print(res) return res ''' ''' diction = {} diction[\"pos\"], diction[\"neg\"] = 0, 0 res = 0 for num in nums: if num == 0: diction[\"pos\"], diction[\"neg\"] = 0, 0 elif num > 0: diction[\"pos\"] += 1 if diction[\"neg\"] % 2 == 0: res = max(res, diction[\"pos\"]+diction[\"neg\"]) elif num < 0: diction[\"neg\"] += 1 if diction[\"neg\"] % 2 == 1: res = max(res, diction[\"pos\"]+diction[\"neg\"]-1) else: res = max(res, diction[\"pos\"]+diction[\"neg\"]) print(res) return res '''
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
from typing import List class Solution: def getMaxLen(self, nums: List[int]) -> int: left_size = [] left_mult = [] size = 0 mult = 0 for val in nums: if val == 0: size = 0 mult = 0 else: size += 1 mult += (1 if val < 0 else 0) left_size.append(size) left_mult.append(mult) right_size = [] right_mult = [] size = 0 mult = 0 for val in nums[::-1]: if val == 0: size = 0 mult = 0 else: size += 1 mult += (1 if val < 0 else 0) right_size.append(size) right_mult.append(mult) right_size = right_size[::-1] right_mult = right_mult[::-1] ans = 0 for idx, val in enumerate(nums): if val == 0: continue ls = 0 lm = 0 if idx > 0: ls = left_size[idx - 1] lm = left_mult[idx - 1] rs = 0 rm = 0 if idx < len(nums) - 1: rs = right_size[idx + 1] rm = right_mult[idx + 1] cur = int(val < 0) if lm % 2 == 0: ans = max(ans, ls) if val > 0: ans = max(ans, 1) if (lm + cur) % 2 == 0: ans = max(ans, ls + 1) if rm % 2 == 0: ans = max(ans, rs) if (rm + cur) % 2 == 0: ans = max(ans, rs + 1) if (lm + cur + rm) % 2 == 0: ans = max(ans, ls + rs + 1) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def _getMaxLen(nums): if not nums: return 0 n_ones = sum(nums) if n_ones % 2 == 0: return len(nums) return len(nums) - min(nums.index(1), nums[::-1].index(1)) - 1 ans = prev = 0 nums = [0 if i > 0 else (1 if i < 0 else -1) for i in nums] while nums: try: idx = nums.index(-1) ans = max(_getMaxLen(nums[:idx]), ans) nums = nums[idx+1:] except: ans = max(ans, _getMaxLen(nums)) break return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def conv(n): if n > 0: return 1 if n == 0: return 0 return -1 nums = list(map(conv, nums)) n = len(nums) def check(size): if size == 0: return True if size > n: return False p = 1 lo = hi = 0 while hi < n: if nums[hi] == 0: p = 1 lo = hi = hi + 1 continue p *= nums[hi] if hi - lo + 1 == size: if p > 0: return True p //= nums[lo] lo += 1 hi += 1 return False res = 0 lo, hi = 0, n while lo <= hi: m = (lo + hi) // 2 r1, r2 = check(m), check(m + 1) if r1 or r2: if r1: res = m if r2: res = m + 1 lo = m + 2 else: hi = m - 1 return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: left = 0 ans = 0 from numpy import sign while left < len(nums) - ans: right = left s = 1 #nextleft = left while right < len(nums): s *= sign(nums[right]) if s > 0: ans = max(ans, right - left + 1) elif s == 0: #nextleft = right break right += 1 if nums[left]>0: while left < len(nums) and nums[left]>0: left += 1 else: left += 1 return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: sublists = [] i = 0 while nums and i < len(nums): if nums[i]: i += 1 else: sublists.append(nums[:i]) nums = nums[i+1:] i = 0 sublists.append(nums) return max([self.getMaxLenFromNonZero(sublist) for sublist in sublists]) def getMaxLenFromNonZero(self, nums: List[int]) -> int: count = 0 front = len(nums) back = 0 for i in range(len(nums)): if nums[i] < 0: count += 1 if front > i: front = i back = i if count % 2 == 0: return len(nums) else: return len(nums) - min([front+1, len(nums) - back])
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def _getMaxLen(nums): if not nums: return 0 fn = 0 while fn < len(nums) and nums[fn] > 0: fn += 1 pos = True res = 0 for i in range(len(nums)): if nums[i] < 0: pos = not pos if pos: res = max(res, i+1) else: res = max(res, i - fn) return res ans = prev = 0 while nums: try: idx = nums.index(0) ans = max(_getMaxLen(nums[:idx]), ans) nums = nums[idx+1:] except: ans = max(ans, _getMaxLen(nums)) break return ans