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: lastExtraMinus = None lastZero=0 out=0 firstExtraMinus = None for i,n in enumerate(nums): if n < 0: lastExtraMinus = None if lastExtraMinus != None else i if firstExtraMinus==None: firstExtraMinus = i if n ==0: lastZero = i+1 lastExtraMinus = None firstExtraMinus = None else: if lastExtraMinus == None: out = max(out,i-lastZero+1) else: out = max(out,i-(firstExtraMinus )) return out
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 find_list(nums): pos = 0 neg = 0 for i in nums : if i > 0 : pos += 1 if i < 0 : neg += 1 if (neg %2 ==0 ): print(len(nums)) else: #find the first negative and remove the list from there and update array first = 0 last = len(nums)-1 while first<=last : #print (first,last,nums[first],nums[last]) if nums[first] <0 : nums = nums[first+1:] break else : first += 1 if nums[last] <0 : nums = nums[:last] break else : last -= 1 return len(nums) #--------------------------------------------- ans = 0 zero = 0 remove = 0 #count positive , negative and 0 numbers for i in nums : if i ==0 : zero += 1 while (zero != 0 ): #remove the array after or before 0 , depending on whats short # update the array first = 0 last = len(nums)-1 while first<=last : if nums[first] ==0 : remove_list = nums[:first] ans = max(ans,find_list(remove_list)) nums = nums[first+1:] break else : first += 1 if nums[last] == 0 : remove_list = nums[last+1:] ans = max(ans,find_list(remove_list)) nums = nums[:last] break else : last -= 1 zero = zero - 1 return max(ans,(find_list(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: def max_len(i, j): if i > j: return 0 res = 1 for k in range(i, j+1): res *= nums[k] if res > 0: return j - i + 1 l = i r = j while nums[l] > 0: l += 1 while nums[r] > 0: r -= 1 return max(j-l, r-i) prev = 0 res = 0 for i in range(len(nums)): if nums[i] == 0: res = max(res, max_len(prev, i-1)) prev = i+1 res = max(res, max_len(prev, len(nums) -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: zeroIdx = [] for idx, num in enumerate(nums): if num == 0: zeroIdx += idx, start = 0 ans = 0 for idx in zeroIdx: if idx - start > 0: ans = max(ans, self.maxLength(nums, [start, idx], idx)) start = idx + 1 ans = max(ans, self.maxLength(nums, [start, len(nums)], len(nums))) return ans def maxLength(self, nums, arr, N): product = 1 Len = 0 for i in range(arr[0], arr[1]): product *= nums[i] # If product is greater than # zero, return array size if (product >= 0): return arr[1]-arr[0] # Traverse the array and if # any negative element found # then update the Length of # longest subarray with the # Length of left and right subarray for i in range(arr[0], arr[1]): if (nums[i] < 0): Len = max(Len,max(N - i - 1, i-arr[0])) return 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: nums += [0] max_so_far = 0 N = len(nums) res = 1 last_zero, last_neg , first_neg, last_pos = 0, -1, N, -1 for i in range(N): res *= nums[i] if nums[i] == 0: if res > 0: max_so_far = max(max_so_far, i-last_zero+1) else: max_so_far = max(max_so_far, i-first_neg, last_pos-last_zero+1) last_zero = i+1 first_neg = N last_pos = last_zero if i < N-1 and nums[i+1] > 0 else last_zero-1 res = 1 elif nums[i] < 0 and first_neg == N: first_neg = i+1 elif res > 0: last_pos = i return max_so_far
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 pos, neg = -1, None curr = 1 for i, n in enumerate(nums): curr *= n if curr == 0: pos, neg = i, None curr = 1 elif curr < 0: if neg is None: neg = i ret = max(ret, i - neg) else: ret = max(ret, i - pos) #print(i, curr, pos, neg, ret) 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: res, n, q, cur = 0, len(nums), [], 1 def extract(q, cur, res): if cur > 0: res = max(res, len(q)) else: i = 0 while i < len(q): if q[i] < 0: break i += 1 res = max(res, len(q) - i - 1) i = len(q) - 1 while i >= 0: if q[i] < 0: break i -= 1 res = max(res, i) return res for v in nums: if v != 0: q.append(v) cur *= v else: res = extract(q, cur, res) cur = 1 q = [] return extract(q, cur, 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: start_neg = -1 end_neg = -1 start_idx = -1 end_idx = -1 acc_product = None ans = 0 nums += [0] for idx, num in enumerate(nums): if num == 0: if start_idx == -1: continue if acc_product > 0: ans = max(ans, end_idx - start_idx + 1) else: if start_neg - start_idx < end_idx - end_neg: ans = max(ans, end_idx - start_neg -1 + 1) else: ans = max(ans, end_neg - start_idx -1 + 1) start_idx = -1 end_idx = -1 acc_product = None start_neg =-1 end_neg=-1 else: if start_idx<0: start_idx=idx end_idx = idx if not acc_product: acc_product =1 acc_product *= num if num < 0: end_neg = idx if start_neg < 0: start_neg =idx # print(start_idx,end_idx,acc_product,start_neg,end_neg) 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: target = [] t = [] for n in nums: if n != 0: t.append(n) else: target.append(t.copy()) t = [] target.append(t.copy()) def find(nums): if len(nums) == 0: return 0 positive_ma = -1 positive_min = -1 negtive_ma = -1 negtive_mi = -1 t = 1 for i in range(len(nums)): t *= nums[i] #print(t) if t > 0 and positive_min == -1: positive_ma = i positive_min = i elif t > 0 and positive_min!=-1: positive_ma = i if t < 0 and negtive_mi == -1: negtive_mi = i negtive_ma = i elif t < 0 and negtive_mi!=-1: negtive_ma = i p_l = 0 n_l = 0 if positive_min != -1: p_l = positive_ma+ 1 if negtive_mi != -1 and negtive_mi!=negtive_ma: n_l = negtive_ma - negtive_mi print((positive_ma,positive_min,negtive_ma,negtive_mi)) return max(p_l,n_l) result = 0 for t in target: result = max(result,find(t)) 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 getMaxLen(self, nums: List[int]) -> int: res, q, n, start = 0, [], len(nums), 0 for i, v in enumerate(nums): if v == 0: start = i + 1 q = [] elif v < 0: q.append(i) if len(q) & 1: res = max(res, i - q[0], q[-1] - start) else: res = max(res, i - start + 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: res,curr,prevp,prevn = 0,1,0,None for i,num in enumerate(nums,1): if num==0: curr,prevp,prevn = 1,i,None else: curr*=num #print(curr,i,prevp,prevn) if curr>0: res = max(res,i-prevp) else: if prevn==None: prevn = i else: res = max(res,i-prevn) 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 = -1, None P, Max = 1, 0 for i in range(len(nums)): P *= nums[i] if P == 0: pos, neg = i, None P = 1 elif P < 0 and neg is None: neg = i elif P < 0: Max = max(Max, i - neg) else: Max = max(Max, i - pos) 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: product = 1 count = 0 firstNegative = -1 maxCount = 0 for i in range(len(nums)): if nums[i] < 0: if firstNegative == -1: firstNegative = i if nums[i] == 0: count = 0 product = 1 firstNegative = -1 else: product*=nums[i] count+=1 if product > 0: maxCount = max(maxCount, count) else: if firstNegative!=-1: maxCount = max(maxCount, i-firstNegative) return maxCount
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: subarrays = [] temp = [] for i in range(len(nums)): if nums[i] == 0: subarrays.append(temp.copy()) temp = [] else: temp += [nums[i]] subarrays.append(temp.copy()) def util(a): p = 1 for i in a: p *= i if p > 0: return len(a) else: i = 0 while a[i] > 0: i += 1 ans1 = len(a) - i - 1 j = len(a) - 1 while a[j] > 0: j -= 1 ans2 = j return max(ans1, ans2) maxm = -1 for i in subarrays: maxm = max(maxm, util(i)) return maxm
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_neg = -1 end_neg = -1 start_idx = -1 end_idx = -1 acc_product = None ans = 0 nums += [0] for idx, num in enumerate(nums): if num == 0: if start_idx == -1: continue if acc_product > 0: ans = max(ans, end_idx - start_idx + 1) else: if start_neg - start_idx < end_idx - end_neg: ans = max(ans, end_idx - start_neg -1 + 1) else: ans = max(ans, end_neg - start_idx -1 + 1) start_idx = -1 end_idx = -1 acc_product = None start_neg =-1 end_neg=-1 else: if start_idx<0: start_idx=idx end_idx = idx if not acc_product: acc_product =1 acc_product *= num if num < 0: end_neg = idx if start_neg < 0: start_neg =idx # print(start_idx,end_idx,acc_product,start_neg,end_neg) 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 check(arr) -> int: if not arr: return 0 pos = neg = -1 pre_prod = 1 res = 0 for i,a in enumerate(arr): pre_prod *= a if pre_prod > 0: if pos == -1: pos = i res = i+1 else: if neg == -1: neg = i res = max(res, i-neg) return res res = i = j = 0 while j < len(nums): while j < len(nums) and nums[j] == 0: j += 1 i = j while j < len(nums) and nums[j] != 0: j += 1 # print(str(i)+'->'+str(j)) res = max(res, check(nums[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: def check(arr) -> int: if not arr: return 0 pos = neg = -1 pre_prod = 1 res = 0 for i,a in enumerate(arr): pre_prod *= a if pre_prod > 0: if pos == -1: pos = i res = i+1 else: if neg == -1: neg = i res = max(res, i-neg) return res res = i = j = 0 while j < len(nums): while j < len(nums) and nums[j] == 0: j += 1 i = j while j < len(nums) and nums[j] != 0: j += 1 # print(str(i)+'->'+str(j)) if j-i > res: res = max(res, check(nums[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: # [1,2,3,-1,-2,3,0,-3,-4,1,2] # [*,*,*,*,*,*,-,*,*,*,*] # [-1,1,1,2,3] # [-,*,*,*,*] cp=1 fp=-1 fn=None ans=0 for i,n in enumerate(nums): cp=n*cp if cp<0: if fn is None: fn=ln=i else: ln=i ans=max(ln-fn,ans) elif cp>0: lp=i ans=max(lp-fp,ans) if n==0: cp=1 fp=i fn=None 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: prod = 1 pos, neg = -1, None ans = 0 for i in range(len(nums)): if nums[i] == 0: pos, neg = i, None prod = 1 else: prod *= nums[i] if prod > 0: ans = max(ans, i - pos) else: if neg == None: neg = i ans = max(ans, i - neg) 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 reduce class Solution: def getMaxLen(self, nums: List[int]) -> int: # assert self.split([1,2,3,0,4,5,6]) == [[1,2,3], [4,5,6]] # assert self.split([1,2,3,0,0,4,5,6]) == [[1,2,3], [4,5,6]] # assert self.split([1]) == [[1]] # assert self.split([1, 0]) == [[1]] # assert self.split([0, 1, 0]) == [[1]] # assert self.split([0,1,-2,-3,-4]) == [[1,-2,-3,-4]] splitted = sorted(self.split(nums), key=lambda x: len(x), reverse=True) # print(splitted) v = 0 for sp in splitted: if len(sp) < v: break v = max(v, self.solve(sp)) return v def split(self, nums): prev = None ans = [] hit = False for i, n in enumerate(nums): # print(i, n, prev) if n == 0 and prev is not None: ans.append(nums[prev:i]) hit = True prev = None elif prev is None and n != 0: prev = i if not hit: return [[n for n in nums if n != 0]] if prev is not None and prev != len(nums) - 1: ans.append(nums[prev:len(nums)]) # print(ans) return ans def solve(self, nums): # print('nums', nums) N = len(nums) prod = reduce(lambda x, y: x * y, nums, 1) if prod > 0: return N # print('prod', prod) for i in range(N // 2): if nums[i] < 0 or nums[N-1-i] < 0: return N - i - 1 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: ans = 0 fn = -1 s = -1 p = 1 for i in range(len(nums)): if nums[i] == 0: fn = -1 s = -1 p = 1 else: if s == -1: s = i p *= nums[i] if p < 0 and fn == -1: fn = i if p < 0: ans = max(ans, (i - s + 1) - (fn - s + 1)) elif p > 0: ans = max(ans, i - s + 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: product = 1 count = 0 firstNegative = -1 maxCount = -math.inf for i in range(len(nums)): if nums[i]>0: product*=nums[i] count+=1 elif nums[i] < 0: product*=nums[i] count+=1 if firstNegative == -1: firstNegative = i if nums[i] == 0: count = 0 product = 1 if firstNegative!=-1 and i-firstNegative-1 > maxCount: maxCount = i-firstNegative-1 firstNegative = -1 if product > 0: maxCount = max(maxCount, count) if firstNegative!=-1 and i-firstNegative > maxCount: maxCount = i-firstNegative return maxCount
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 calc(self, neg: List[int], i:int) -> int: if len(neg) % 2 == 0: return i else: return max(i-neg[0]-1, neg[-1]) def getMaxLen(self, nums: List[int]) -> int: ml = 0 while True: neg = [] for idx,i in enumerate(nums): if i == 0: ml = max(self.calc(neg, idx), ml) nums = nums[idx+1:] break elif i < 0: neg.append(idx) else: if nums != []: ml = max(ml, self.calc(neg, idx+1)) break return ml
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) firstNegIdx = math.inf prevZeroIdx = -1 curr = 1 res = 0 for i, n in enumerate(nums): if n==0: prevZeroIdx = i firstNegIdx = math.inf curr = 1 else: curr *= n if curr>0: res = max(res, i-prevZeroIdx) else: firstNegIdx = min(firstNegIdx, i) res = max(res, i-firstNegIdx) # print (i, curr, res) return res # if math.isfinite(res): # return res # else: # 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: accLast = 1 accCurr = 1 lastpos = 0 lastnegative = -1 res = 0 dic = {} for i in range(len(nums)): accCurr = accLast*nums[i] if accCurr == 0: lastpos = i+1 lastnegative = -1 accCurr = 1 elif accCurr > 0: if lastpos != -1: # print(i, lastpos) res = max(res, i+1-lastpos) else: if lastnegative != -1: # print('-', i, lastnegative) res = max(res, i+1-lastnegative) else: lastnegative = i+1 accLast = accCurr # print(acc) 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: start_neg, end_neg, start_idx, end_idx = [-1,-1,-1,-1] acc_product = None ans = 0 nums += [0] for idx, num in enumerate(nums): if num == 0: if start_idx == -1: continue if acc_product > 0: ans = max(ans, end_idx - start_idx + 1) else: if start_neg - start_idx < end_idx - end_neg: ans = max(ans, end_idx - start_neg -1 + 1) else: ans = max(ans, end_neg - start_idx -1 + 1) start_neg, end_neg, start_idx, end_idx = [-1,-1,-1,-1] acc_product = None else: if start_idx<0: start_idx=idx end_idx = idx if not acc_product: acc_product =1 acc_product *= num if num < 0: end_neg = idx if start_neg < 0: start_neg =idx # print(start_idx,end_idx,acc_product,start_neg,end_neg) 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: fpos = -1 fneg = -1 best = 0 cur = 1 for i in range(len(nums)): n = nums[i] if n == 0: fpos = i fneg = -1 cur = 1 else: if fneg == -1 and n < 0: fneg = i cur *= n if cur > 0: best = max(best, i - fpos) elif cur < 0 and fneg != -1: best = max(best, i - fneg) 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: max_len = 0 start = -1 is_pos = True for i in range(len(nums) + 1): if i == len(nums) or nums[i] == 0: if is_pos: max_len = max(max_len, i - start - 1) else: tmp = start + 1 while tmp < i: if nums[tmp] < 0: break tmp += 1 max_len = max(max_len, i - tmp - 1) start = i is_pos = True else: if nums[i] < 0: is_pos = not is_pos if is_pos: max_len = max(max_len, i - start) # print(start, i, is_pos, max_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: lz = [i for (i,v) in enumerate(nums) if v < 0] ts = len(nums) if len(lz) % 2 == 0: return ts if len(lz) == 1: lz = [lz[0],lz[0]] ls = lz[-1] rs = ts-(lz[0]+1) if ls > rs: return ls return rs def getMaxLen(self, nums: List[int]) -> int: r = 0 zero1 = 0 try: zero1 = nums.index(0) r = self._getMaxLen(nums[:zero1]) nums = nums[zero1+1:] while True: zero2 = nums.index(0) r2 = self._getMaxLen(nums[:zero2]) if r2 > r: r = r2 zero1 = zero2 nums = nums[zero1+1:] except ValueError: pass r2 = self._getMaxLen(nums) if r2 > r: r = r2 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: if not nums: return 0 # index of last neg accumulated product last_neg_i = -1 acc_product = 1 bound = -1 max_len = 0 for i in range(len(nums)): if nums[i] == 0: acc_product = 1 last_neg_i = -1 bound = i continue acc_product = acc_product * nums[i] if last_neg_i == -1 and acc_product < 0: last_neg_i = i if acc_product < 0: max_len = max(max_len, i - last_neg_i) else: max_len = max(max_len, i - bound) 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: ''' new array = [(st, curr_prod_from_st)] if curr_prod_from_st > 0: max_sub = max(max_sub, ed - st + 1) elif: max_sub = max(max_sub, ed - idx_of_last_time_curr_prod_was_neg) ''' max_len = 0 last_idx = float('inf') curr_st, curr_prod = 0, 1 for ed in range(len(nums)): if nums[ed] == 0: curr_prod = 1 curr_st = ed + 1 last_idx = float('inf') continue else: curr_prod *= nums[ed] if curr_prod > 0: max_len = max(max_len, ed - curr_st + 1) elif curr_prod < 0: max_len = max(max_len, ed - last_idx) if last_idx == float('inf'): last_idx = ed 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: last={1:-1} pro=1 ans=0 for i,c in enumerate(nums): pro=c*pro if pro>0: ans=max(i-last[1],ans) elif pro<0: if -1 in last: ans=max(ans,i-last[-1]) else: last[-1]=i elif pro==0: pro=1 last={} last[1]=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: first_neg = None last_neg = None first_pos = None cum_prod = 1 max_len = 0 last_zero_idx = 0 for idx, val in enumerate(nums): if val == 0: first_neg = None last_neg = None first_pos = None cum_prod = 1 last_zero_idx = idx + 1 else: cum_prod *= val if cum_prod > 0: max_len = max(max_len, idx - last_zero_idx + 1) else: if first_neg is None: first_neg = idx last_neg = idx max_len = max(max_len, last_neg - first_neg) 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: i = 0 negatives = [-1, -1] p = 1 ans = 0 for j, n in enumerate(nums): if not n: p = 1 i = j + 1 negatives = [-1, -1] else: p *= n if n < 0: if negatives[0] < 0: negatives[0] = j negatives[1] = j if p > 0: ans = max(ans, j - i + 1) else: ans = max(ans, negatives[1] - i, j - negatives[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: longest = 0 neg = len(nums) pos = -1 prod = 1 for i, n in enumerate(nums): if not n: neg = len(nums) pos = i prod = 1 else: prod *= n if prod < 0: neg = min(neg, i) longest = max(longest, i - neg) else: longest = max(longest, i - pos) return longest
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 helper(nums): if len(nums) == 0: return 0 pre_prod = 1 N = len(nums) first_neg = -1 res = 0 for i in range(N): pre_prod *= nums[i] if pre_prod > 0: res = max(res, i + 1) elif first_neg == -1 and pre_prod < 0: first_neg = i elif pre_prod < 0: res = max(res, i - first_neg) return res num = [] res = 0 for i in range(len(nums)): if nums[i] == 0: res = max(res, helper(num)) num = [] else: num.append(nums[i]) res = max(res, helper(num)) 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, A: List[int]) -> int: n = len(A) lastZ = -1 lastN = n cur = 1 res = 0 for i, a in enumerate(A): cur *= a if cur == 0: lastZ = i lastN = n cur = 1 elif cur > 0: res = max(res, i - lastZ) else: lastN = min(lastN, i) res = max(res, i - lastN) 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_len, start, product, first_minus_index = 0, 0, 1, -1 for i, n in enumerate(nums): if n == 0: start, product, first_minus_index = i + 1, 1, -1 else: if n < 0 and first_minus_index == -1: first_minus_index = i product *= n if n > 0: max_len = max(max_len, 1) if product > 0: max_len = max(max_len, i - start + 1) if product < 0 and first_minus_index != -1: max_len = max(max_len, i-first_minus_index) 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: s = 0 fn = -1 ln = -1 cn = 0 res = 0 for i, v in enumerate(nums): if v == 0: end = i - 1 if cn % 2 == 0: res = max(res, end - s + 1) else: res = max(res, end - fn, ln - s) s = i + 1 fn = -1 ln = -1 cn = 0 elif v < 0: if fn == -1: fn = ln = i else: ln = i cn += 1 end = len(nums) - 1 if cn % 2 == 0: res = max(res, end - s + 1) else: res = max(res, end - fn, ln - s) 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, a: List[int]) -> int: n = len(a) ans = 0 neg = -1 #position of last negative element zero = -1 #position of last zero cnt = 0 #number of negatives seen so far for i, x in enumerate(a): if x == 0: neg = zero = i cnt = 0 elif x < 0: if neg == zero: neg = i cnt += 1 if cnt % 2 == 0: ans = max(ans, i - zero) else: ans = max(ans, i - neg) 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: left_pos = 0 left_neg = sys.maxsize ans = 0 cp = 1 for i, num in enumerate(nums): if num == 0: cp = 1 left_pos = i + 1 left_neg = sys.maxsize else: cp *= num # print(\"haha\", cp) if cp > 0: ans = max(ans, i - left_pos + 1) # print(\"hehe\", ans, num, cp) else: ans = max(ans, i - left_neg) # print(\"hehe\", ans, num, cp, left_neg) if cp < 0 and left_neg == sys.maxsize: left_neg = 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: a = [None] + nums n = len(a) pos = [0] * n neg = [0] * n for i in range(1, n): if a[i] > 0: pos[i] = pos[i - 1] + 1 if neg[i - 1]: neg[i] = neg[i - 1] + 1 # we can extend neg iff it exists elif a[i] < 0: if neg[i - 1]: pos[i] = neg[i - 1] + 1 # same idea as above neg[i] = pos[i - 1] + 1 else: # a[i] == 0 pass 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] * 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: 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: f = [0, 0] ans = 0 for i in range(len(nums)): if nums[i] < 0: f = [f[1] + 1 if f[1] > 0 else 0, f[0] + 1] elif nums[i] > 0: f = [f[0] + 1, f[1] + 1 if f[1] > 0 else 0] else: f = [0, 0] ans = max(ans, f[0]) #print(f) 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 = 0 pos, neg = 0, 0 for i in range(len(nums)): if nums[i] > 0: pos = pos + 1 neg = neg + 1 if neg > 0 else 0 elif nums[i] < 0: pos, neg = neg+1 if neg > 0 else 0, pos+1 else: pos, neg = 0, 0 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
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos = [0 for _ in range(n)] neg = [0 for _ in range(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(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: # 类似于前缀和 看区间 # sums 记录从第一个不是0 开始 到当前index的正负情况 # 如果sums 是负的话,看之前有没有negative 的index # 如果有,减去上一个negative 的index, 上一个negative index 到 当前index 整个区间就变成正的了 # 如果没有,记录当前的index 更新negative # 如果sums 是正的话, 直接更新,当前idnex 减去 上一个0 的位置,或者 初始化 -1 # 如果遇到0,全部初始化 重新计算 positive_index = - 1 negative_index = - 1 sums = 1 result = 0 for i in range(len(nums)): if sums * nums[i] < 0: sums = -1 elif sums * nums[i] > 0: sums = 1 if nums[i] == 0: positive_index = i negative_index = -1 sums = 1 elif sums > 0: result = max(result, i - positive_index) else: if negative_index == -1: negative_index = i else: result = max(result, i - negative_index) print(result) 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 getMaxLen(self, nums: List[int]) -> int: N =len(nums) ans = 0 i = j = 0 while i<N: if nums[i] == 0: i+= 1 continue j = i while j < N and nums[j]: j+= 1 negs = 0 firstneg = lastneg = None for k in range(i,j ): if nums[k] <0: negs += 1 if firstneg is None: firstneg = k lastneg = k if negs % 2 ==0: ans = max(ans, j-i) else: ans = max(ans, firstneg -i, j-i-1-(firstneg-i)) ans = max(ans, lastneg -i, j-i-1-(lastneg-i)) i = j 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 for r in [list(range(0, len(nums))), list(range(len(nums)-1, -1, -1))]: cur_len = 0 cur_sign = 1 for i in r: if nums[i] > 0: cur_len += 1 result = max(result, cur_sign * cur_len) elif nums[i] == 0: cur_len = 0 cur_sign = 1 else: # < 0 cur_len += 1 cur_sign = -cur_sign result = max(result, cur_sign * cur_len) 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 getMaxLen(self, nums: List[int]) -> int: first_neg = -1 last_neg = -1 pos = True start = 0 best = 0 n = len(nums) for i, num in enumerate(nums): if num == 0: if pos: best = max(best, i - start) else: if first_neg >= start: best = max(best, i - first_neg - 1, last_neg - start) start = i + 1 pos = True elif num < 0: last_neg = i if first_neg < start: first_neg = i pos = not pos if pos: best = max(best, n - start) else: if first_neg >= start: best = max(best, n - first_neg - 1, last_neg - start) 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: 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,len(nums)): if nums[i] > 0: neg[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 pos[i] = 1 + pos[i - 1] if 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: result = 0 cur_len = 0 cur_sign = 1 for i in range(0, len(nums)): if nums[i] > 0: cur_len += 1 result = max(result, cur_sign * cur_len) elif nums[i] == 0: cur_len = 0 cur_sign = 1 else: # < 0 cur_len += 1 cur_sign = -cur_sign result = max(result, cur_sign * cur_len) cur_len = 0 cur_sign = 1 for i in range(len(nums)-1, -1, -1): if nums[i] > 0: cur_len += 1 result = max(result, cur_sign * cur_len) elif nums[i] == 0: cur_len = 0 cur_sign = 1 else: # < 0 cur_len += 1 cur_sign = -cur_sign result = max(result, cur_sign * cur_len) 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 getMaxLen(self, nums: List[int]) -> int: #------------------------------ def helper(array): if len(array)==0: return 0 result=0 pos=0 neg=0 for n in array: if n>0: pos, neg = pos+1, neg+1 if neg>0 else 0 if n<0: pos, neg = neg+1 if neg>0 else 0, pos+1 result=max(result,pos) return result #------------------------------ arrays=[] subarray=[] for n in nums: if n==0: idx=0 arrays.append(subarray) subarray=[] continue subarray.append(n) if len(subarray)>0: arrays.append(subarray) maxlen=0 for i,array in enumerate(arrays): maxlen=max(maxlen,helper(array)) 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: start = 0 end = 0 count_negative = 0 max_len = 0 while end < len(nums): if nums[end] == 0: if count_negative % 2 != 0: while nums[start] > 0: start += 1 max_len = max(max_len, end - start - 1) start = end = end + 1 count_negative = 0 else: if nums[end] < 0: count_negative += 1 if count_negative % 2 == 0: max_len = max(max_len, end - start + 1) if end == len(nums)-1 and count_negative % 2 == 1: while nums[start] > 0: start += 1 max_len = max(max_len, end - start) end += 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: count_of_negative = 0 start = 0 max_length = 0 def evaluate_sub_array(begin, end, count_of_negative): if count_of_negative % 2 == 0: return end-begin+1 else: first_negative = len(nums) last_negative = -1 for i in range(begin,end+1): if nums[i] < 0: if first_negative == len(nums): first_negative = i last_negative = i else: last_negative = i return max(end - first_negative, last_negative - begin) start = 0 array = [] count_of_negative = 0 for index in range(len(nums)): if nums[index] <0: count_of_negative+=1 if nums[index] == 0: array.append(evaluate_sub_array(start, index-1, count_of_negative)) count_of_negative = 0 start = index+1 array.append(evaluate_sub_array(start, len(nums)-1, count_of_negative)) return max(array)
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, 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: pos, neg = 1 + neg if neg > 0 else 0, 1 + pos else: pos, neg = 0, 0 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
class Solution: def getMaxLen(self, nums: List[int]) -> int: dic={1:-1} temp=1 res=0 for i in range(len(nums)): if nums[i]==0: dic={1:i} temp=1 continue if nums[i]<0: temp=-temp if not dic.get(temp) is None: res=max(res,i-dic[temp]) else: dic[temp]=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: #reduce to simpler problem: # if all elems are positive, then entire array is sol as long as thy're >= 1 # an even number of negative vals still gives a pos result # insight: we don't care what the values actually are , just whether they're pos, 0 , or neg # insight: we can't include a zero, since the prod becomes 0, and 0 isn't +ve # idea: start with first char, if you hit zero, stop, if not, keep going, and track latest position of even number of negatives. # also start at other negative vals, and positive vals after zero. # cur_max = 0 # last_even_neg = -1 # for i, n in enumerate(nums): # if n == 0: # if last_even_neg > # if i == 0 or (i and nums[i-1] == 0) # first, split the array into segment to get rid of zeros. def max_len(nums): negs = [i for i, x in enumerate(nums) if x < 0] n = len(nums) if len(negs) % 2 == 0: # even num of odd nums, whose product is pos return n else: # at least one neg val return max(n - (negs[0] + 1), negs[-1]) # exclude either left or right most neg val from copy import deepcopy chunks = [] chunk = [] for x in nums: if x == 0 and chunk: # see a zero, add a new segment chunks.append(deepcopy(chunk)) chunk.clear() elif x: chunk.append(x) chunks.append(chunk) print(chunks) return max(map(max_len, chunks))
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: m=0 neg=0 pos=0 positiveBeforeFirstNeg=0 pos2=0 for i in nums: if(i==0): if(neg>0 and neg%2!=0): neg-=1 if(positiveBeforeFirstNeg<pos2 and neg>2): pos-=positiveBeforeFirstNeg pos+=pos2 m=max(max(pos,pos2)+neg,m) neg,pos,pos2=0,0,0 elif(i<0): if(neg==0): positiveBeforeFirstNeg=pos neg+=1 if(neg%2==0): pos+=pos2 pos2=0 else: if(neg%2!=0): pos2+=1 else: pos+=1 if(positiveBeforeFirstNeg<pos2 and neg>2): pos-=positiveBeforeFirstNeg pos+=pos2 if(neg>0 and neg%2!=0): neg-=1 m=max(max(pos,pos2)+neg,m) 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: # encouter 0 reset it as 0 # how many -1 in the left, if is even, update the longest length O(n) cur_poistion - 0_position # [1,-2,-3,4] => [0, 1, 2, 2] => 4 # [0,1,-2,-3,-4] => [0, 0, 1, 2, 3] => 3 # [-1,-2,-3,0,1] => [1, 2, 3, 0, 0] => 2 # [-1,2] => [1, 1] => 1 (consider if itself > 0, 1) # [1,2,3,5,-6,4,0,10] => [0, 0, 0, 0, 1, 1, 0, 1] => 4 # [0, 0 ,0 ,0] => [0, 0, 0, 0] => 0 (consider if itself == 0, 0) carry_len = 0 left_neg = 0 max_len = 0 str_ptr = -1 for idx, n in enumerate(nums): if n < 0 :left_neg += 1 carry_len += 1 if n == 0: max_len = max(max_len, 0) carry_len = 0 left_neg = 0 else: max_len = max(max_len, 1 if n > 0 else 0, idx - str_ptr -1, carry_len if left_neg % 2 == 0 else 0) if left_neg == 0: str_ptr = idx 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: start, curnegcnt, res, fnegpos, lnegpos = 0, 0, 0, 0, 0 flag = True i, j = 0, len(nums) while i < j: if nums[i] == 0: flag = True i += 1 start = i curnegcnt = 0 continue if nums[i] < 0: if flag: flag = False fnegpos = i lnegpos = i curnegcnt += 1 if curnegcnt == 0 or curnegcnt % 2 == 0: res = max(res, (i - start) + 1) else: res = max(res, (lnegpos ) - start, i - (fnegpos )) 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: #每次maintain,到之前为止,正的有多长,负的有多长,然后maintain max len pos,neg = 0,0 asr = 0 for num in nums: if num == 0: pos,neg = 0,0 elif num > 0: pos+=1 neg = neg + 1 if neg else 0 else: n_pos = neg + 1 if neg else 0 n_neg = pos + 1 if pos else 1 pos,neg = n_pos,n_neg asr = max(asr,pos) #print(pos,neg) return asr
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
# 1567. Maximum Length of Subarray With Positive Product def sgn (n): if n > 0: return +1 elif n < 0: return -1 else: return 0 def split_0 (arr): arr_buffer = [] for elem in arr: if elem != 0: arr_buffer.append (elem) else: yield arr_buffer arr_buffer = [] assert len (arr_buffer) == 0 def partial_products (arr): prod = 1 yield prod for elem in arr: prod *= elem yield prod def get_subarr_max_len (arr): # zero-free first_index = {} max_len = 0 for (i, prod) in enumerate (partial_products (arr)): first_index.setdefault (prod, i) max_len = max (max_len, i - first_index[prod]) return max_len def get_max_len (arr): arr = [sgn (x) for x in arr] arr.append (0) if len (arr) == 0: return 0 return max (get_subarr_max_len (subarr) for subarr in split_0 (arr)) class Solution: def getMaxLen(self, nums: List[int]) -> int: return get_max_len(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 = [[0, 0] for _ in range(len(nums) + 1)] for i in range(len(nums)): if nums[i] < 0: f[i+1] = [f[i][1] + 1 if f[i][1] > 0 else 0, f[i][0] + 1] elif nums[i] > 0: f[i+1] = [f[i][0] + 1, f[i][1] + 1 if f[i][1] > 0 else 0] #print(f) return max(x[0] for x in 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: n = len(nums) dp_pos = [0] * (n+1) dp_neg = [0] * (n+1) result = 0 for i in range(1, n+1): if nums[i-1] == 0: dp_pos[i] = 0 dp_neg[i] = 0 elif nums[i-1] > 0: dp_pos[i] = 1 + dp_pos[i-1] if dp_neg[i-1] > 0: dp_neg[i] = 1 + dp_neg[i-1] else: dp_neg[i] = 0 else: if dp_neg[i-1] > 0: dp_pos[i] = 1 + dp_neg[i-1] else: dp_pos[i] = 0 dp_neg[i] = 1 + dp_pos[i-1] result = max(result, dp_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 getMaxLen(self, nums: List[int]) -> int: n = len(nums) if not n: return -1 pos = 0 neg = 0 answer = 0 for num in nums: if num == 0: answer = max(answer, pos) pos = neg = 0 continue if num > 0: pos += 1 neg += 1 if neg else 0 else: orig_pos = pos pos = neg+1 if neg else 0 neg = (orig_pos + 1) if orig_pos else 1 answer = max(answer, pos) 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: dct = {0:-1} count = 0 res = 0 for i,num in enumerate(nums): if num <0: count +=1 # print(num,count,i) dct[count]=i elif num == 0: dct = {0:i} count = 0 if count %2 == 0: res = max(res,i-dct[0]) else: t1 = i-dct[1] t2= 0 if num < 0: t2 = i-1-dct[0] res = max(res,max(t1,t2)) # print(dct,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: if nums[-1] != 0: nums.append(0) n = len(nums) first_0 = -1 last_0 = -1 cnt_neg = 0 first_neg = -1 last_neg = -1 res = 0 # print(nums) for i, e in enumerate(nums): if e < 0: cnt_neg += 1 if first_neg == -1: first_neg = i last_neg = i elif e == 0: # print(cnt_neg) last_0 = i if cnt_neg % 2 == 0: res = max(res, last_0-first_0-1) else: res = max(res, last_neg-first_0-1, last_0-first_neg-1) # print((first_0, last_0, first_neg, last_neg, res)) cnt_neg = 0 first_0 = last_0 first_neg = -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: a = 0 p = 0 n = 0 r = -1 for x in nums: p, n, r = ( (x < 0) * p + (x > 0) * (p + 1), (x < 0) * (n + 1) + (x > 0) * n, (x < 0) * (r < 0) * p + (x < 0) * (r >= 0) * r + (x > 0) * r - (x == 0) ) a = max(a, p + n - (n % 2) * (r + 1)) return a
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: # get the next zero or end # get the last idx of negative before zero and end # get how many negative numbers next_zero_or_end=[0]*(len(nums)+1) next_idx=len(nums) last_negative=[0]*(len(nums)+1) negative_idx=None state=0 for i in range(len(nums)-1,-1,-1): num=nums[i] if num==0: next_idx=i state=0 next_zero_or_end[i]=next_idx if state==0 and num<0: negative_idx=i state=1 last_negative[i]=negative_idx ne_nums=[0] * (len(nums)+1) for i,num in enumerate(nums): ne_nums[i]=ne_nums[i-1]+(1 if num<0 else 0) res=0 for i in range(len(nums)): if nums[i]==0: continue j=next_zero_or_end[i]-1 if (ne_nums[j]-ne_nums[i-1])%2==0: res=max(res,(j-i+1)) else: j=last_negative[i]-1 res=max(res,(j-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 Node: def __init__(self, size): self.size = size self.left = None self.right = None class Solution: def getMaxLen(self, nums: List[int]) -> int: nodes = [] roots = [] curr_size = 0 prev = None for i in range(len(nums)): if nums[i] > 0: curr_size += 1 else: node = Node(curr_size) nodes.append(node) if prev: prev.right = node node.left = prev else: roots.append(node) if nums[i] != 0: prev = node else: prev = None curr_size = 0 node = Node(curr_size) if prev: prev.right = node node.left = prev else: roots.append(node) # t_node = roots[0] # while t_node: # print(t_node.size) # t_node = t_node.right longest = 0 for node in roots: first_count = node.size second_count = 0 if not node.right else node.right.size curr_node = node.right.right if node.right else None ind = 0 while curr_node: if ind == 0: first_count += curr_node.size + curr_node.left.size + 2 ind = 1 else: second_count += curr_node.size + curr_node.left.size + 2 ind = 0 curr_node = curr_node.right # print(first_count) # print(second_count) longest = max(longest, first_count, second_count) return longest
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 for _ in range(len(nums))] # pos[i] is number of consecutive numbers with pos product up to i neg = [0 for _ in range(len(nums))] # neg[i] is number of consecutive numbers with neg product up to i if nums[0] > 0 : pos[0] = 1 elif nums[0] < 0: neg[0] = 1 out = 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 elif nums[i] < 0: pos[i] = 1 + neg[i - 1] if neg[i - 1] else 0 neg[i] = 1 + pos[i - 1] out = max(out, pos[i]) return out
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 getMaxLenNonzero(arr): neg = [] for i, num in enumerate(arr): if num < 0: neg.append(i) if len(neg) % 2 == 0: return len(arr) if len(neg) == 1: return max(neg[0], len(arr) - neg[-1] - 1) return max(neg[-1], len(arr) - neg[0] - 1) currArr = [] ans = 0 for num in nums: if num != 0: currArr.append(num) else: ans = max(ans, getMaxLenNonzero(currArr)) currArr = [] if currArr: ans = max(ans, getMaxLenNonzero(currArr)) 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] * len(nums) for i in range(len(dp)): dp[i] = [0,0] ans = 0 for i, v in enumerate(nums): if v > 0: dp[i][0] = 1 + dp[i-1][0] if dp[i-1][1]: dp[i][1] = 1 + dp[i-1][1] if v < 0: if dp[i-1][1]: dp[i][0] = 1 + dp[i-1][1] dp[i][1] = 1 + dp[i-1][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: ans = pos = neg = 0 for x in nums: if x > 0: pos, neg = 1 + pos, 1 + neg if neg else 0 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
class Solution: def getMaxLen(self, nums: List[int]) -> int: A = nums n = len(A) dp = [[0,0] for i in range(n+1)] max_len = 0 for i in range(1,n+1): if A[i-1] > 0: dp[i][0] = dp[i-1][0]+1 dp[i][1] = dp[i-1][1]+1 if dp[i-1][1] != 0 else 0 elif A[i-1] < 0: dp[i][0] = dp[i-1][1]+1 if dp[i-1][1] != 0 else 0 dp[i][1] = dp[i-1][0]+1 else: dp[i][0] = 0 dp[i][1] = 0 max_len = max(max_len, dp[i][0]) # print(dp) 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 total=0 acc=1 start=-1 first=-1 last=0 for i in range(len(nums)): if nums[i]==0: if acc>0: ret=max(total,ret) else: ret=max(ret,first-1,total-first,last-1,total-last) start=i acc=1 total=0 first=-1 last=0 else: total+=1 acc=acc*(1 if nums[i]>0 else -1) if nums[i]<0: if first==-1: first=i-start last=0 last+=1 if acc>0: ret=max(total,ret) else: ret=max(ret,first-1,total-first,last-1,total-last) 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: listt = [] a = -1 for i in range(len(nums)): if nums[i] == 0: listt.append(nums[a + 1:i]) a = i listt.append(nums[a + 1:]) while [] in listt: listt.remove([]) if listt==[]: return 0 recordlist = {} for i in range(len(listt)): firstneg=-1 begneg=-1 recordlist[i]=0 for m in range(len(listt[i])): if listt[i][m]<0 and firstneg==-1: firstneg = m if begneg==-1: begneg=m continue if listt[i][m]<0 and firstneg!=-1: firstneg=-1 if firstneg==-1: recordlist[i]=len(listt[i]) else: recordlist[i]=max([firstneg,len(listt[i])-firstneg-1,begneg,len(listt[i])-begneg-1]) m=[] for i in list(recordlist.values()): m.append(i) return max(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: #Any subarray containing a zero must be broken into seperate subarrays either side of the zero #Any subarr with an even number of negatives can be counted in its entirety #For a subarr with an odd number of negatives, the largest subarray excluding either the left or rightmost negative number can be taken instead def getMaxLen(self, nums: List[int]) -> int: #Simplify array to +-1 for i in nums: if i > 0: i = 1 elif i < 0: i = -1 arrays = [] end = 0 for i in range(len(nums)): if nums[i] == 0: arrays.append(nums[end:i]) end = i + 1 arrays.append(nums[end:]) #print(arrays) maximum = 0 for arr in arrays: maxi = 0 neg = 0 first = -1 for i in range(len(arr)): if arr[i] < 0: neg += 1 if first == -1: first = i + 1 last = i if neg % 2 == 0: maxi = len(arr) else: #Length of sub a (missing first) subA = len(arr) - first #Length of sub b (missing last) subB = last maxi = max(subA, subB) #print(arr, maxi) maximum = max(maximum, maxi) return maximum
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 helper(nums): result = 0 positiveSoFar = 0 negativeSoFar = 0 for i in range(len(nums)): if nums[i] == 0: positiveSoFar = 0 negativeSoFar = 0 elif nums[i] > 0 : positiveSoFar += 1 if negativeSoFar > 0: negativeSoFar += 1 elif nums[i] < 0: if negativeSoFar > 0: positiveSoFar = max(negativeSoFar, positiveSoFar) +1 negativeSoFar = 0 else: negativeSoFar = positiveSoFar + 1 positiveSoFar = 0 result = max(result, positiveSoFar) return result # scan from left and scan from right return max(helper(nums), helper(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: dp = [] for num in nums: if num == 0: dp.append(0) elif num > 0: dp.append(1) else: dp.append(-1) for i in range(1, len(dp)): if dp[i - 1] == 0: continue else: dp[i] *= dp[i - 1] print(dp) dic = {} res = 0 zero = -1 for i,num in enumerate(dp): if num > 0: if 1 not in dic: res = max(res,i - zero) dic[1] = i else: res = max(res, i - dic[1] + 1, i - zero) elif num == 0: dic = {} zero = i else: if -1 not in dic: dic[-1] = i else: res = max(res, i - dic[-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, 0 if nums[0]>0: pos = 1 if nums[0]<0: neg = 1 ans = pos for i in range(1, len(nums)): if nums[i] > 0: pos, neg = pos+1, neg+1 if neg else 0 elif nums[i] < 0: pos, neg = neg+1 if neg else 0, pos+1 else: pos, neg = 0, 0 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
class Solution: def getMaxLen(self, nums: List[int]) -> int: def helper(nums): result = 1 if nums[0] > 0 else 0 positiveSoFar = 1 if nums[0] > 0 else 0 negativeSoFar = 1 if nums[0] < 0 else 0 for i in range(1, len(nums)): if nums[i] == 0: positiveSoFar = 0 negativeSoFar = 0 elif nums[i] > 0 : positiveSoFar += 1 if negativeSoFar > 0: negativeSoFar += 1 elif nums[i] < 0: if negativeSoFar > 0: positiveSoFar = max(negativeSoFar, positiveSoFar) +1 negativeSoFar = 0 else: negativeSoFar = positiveSoFar + 1 positiveSoFar = 0 result = max(result, positiveSoFar) return result return max(helper(nums), helper(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: numPos, numNeg, res = 0, 0, 0 for i in range(len(nums)): if nums[i] > 0: numPos += 1 if numNeg > 0: numNeg += 1 elif nums[i] < 0: numPosTmp, numNegTmp = numPos, numNeg if numPosTmp > 0: numNeg = numPosTmp + 1 else: numNeg = 1 if numNegTmp > 0: numPos = numNegTmp + 1 else: numPos = 0 else: numNeg = 0 numPos = 0 res = max(res, numPos) 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: if not nums: return 0 splittedArrays = [] currentArray = [] negCount = 0 for num in nums: if num == 0: if len(currentArray) > 0: splittedArrays.append((currentArray, negCount)) currentArray = [] negCount = 0 else: currentArray.append(num) negCount += 1 if num < 0 else 0 if len(currentArray) > 0: splittedArrays.append((currentArray, negCount)) if not splittedArrays: return 0 maxLength = 0 for splittedArray, negCount in splittedArrays: if negCount % 2 == 0: maxLength = max(maxLength, len(splittedArray)) else: removedNums = 0 i = 0 while splittedArray[i] > 0: removedNums += 1 i += 1 maxLength = max(maxLength, len(splittedArray) - removedNums - 1) removedNums = 0 i = len(splittedArray) - 1 while splittedArray[i] > 0: removedNums += 1 i -= 1 maxLength = max(maxLength, len(splittedArray) - removedNums - 1) return maxLength
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: neg[0] = 1 elif nums[0] > 0: pos[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: n = len(nums) l = -1 neg_num = 0 result = 0 for i in range(n): if (nums[i] == 0): while (neg_num % 2): if (nums[l + 1] < 0): neg_num -= 1 l += 1 # print(i, l) result = max(result, i - l - 1) l = i count = 0 elif (nums[i] < 0): neg_num += 1 if (neg_num % 2 == 0): result = max(result, i - l) while (neg_num % 2) and (l < n - 1): if (nums[l + 1] < 0): neg_num -= 1 l += 1 # print(l) result = max(result, n - 1 - l) 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 getMaxLen(self, nums: List[int]) -> int: if not nums: return 0 size = len(nums) if size == 1: return 0 if nums[0] < 0 else 1 start = 0 end = 0 longest = 0 while end < size: numNeg = 0 leftNeg = -1 rightNeg = -1 while end < size and not nums[end] == 0: if nums[end] < 0: numNeg += 1 rightNeg = end if leftNeg == -1: leftNeg = end end += 1 if numNeg % 2 == 0: longest = max(longest, end - start) else: longest = max(longest, end - rightNeg - 1, rightNeg - start, end - leftNeg - 1, leftNeg - start) end += 1 start = end return longest
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 = -1, n c = 1 ret = 0 for i, j in enumerate(nums): if j == 0: pos, neg = i, n c = 1 continue if j > 0: pass else: c *= -1 if c == 1: ret = max(ret, i - pos) else: ret = max(ret, i - neg) neg = min(neg, 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: dp = [[0]*2 for _ in range(len(nums))] dp[0][0] = nums[0] > 0 dp[0][1] = nums[0] < 0 ret = dp[0][0] for i in range(1,len(nums)): if nums[i] == 0: continue if nums[i] > 0: dp[i][0] = dp[i-1][0] + 1 dp[i][1] = 0 if not dp[i-1][1] else dp[i-1][1] + 1 else: dp[i][0] = 0 if not dp[i-1][1] else dp[i-1][1] + 1 dp[i][1] = dp[i-1][0] + 1 ret = max(ret,dp[i][0]) return int(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
# 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: # \"pos\", \"neg\" represent longest consecutive numbers ending with nums[i] forming a positive/negative product. 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: # dp[i]: max length of pos subarray ends at i l = len(nums) pdp = [0] * l ndp = [0] * l pdp[0] = 1 if nums[0] > 0 else 0 ndp[0] = 1 if nums[0] < 0 else 0 b = pdp[0] for i in range(1, l): n = nums[i] if n > 0: pdp[i] = max(pdp[i - 1] + 1, 1) ndp[i] = ndp[i - 1] + 1 if ndp[i - 1] != 0 else 0 elif n < 0: ndp[i] = max(pdp[i - 1] + 1, 1) pdp[i] = ndp[i - 1] + 1 if ndp[i - 1] != 0 else 0 else: pdp[i] = 0 ndp[i] = 0 b = max(b, pdp[i]) return b
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] * 2 for _ in range(len(nums))] if nums[0] < 0: dp[0][1] = 1 if nums[0] > 0: dp[0][0] = 1 res = dp[0][0] for i, num in enumerate(nums): if i and num < 0: dp[i][1] = dp[i-1][0] + 1 if dp[i-1][1]: dp[i][0] = dp[i-1][1] + 1 if i and num > 0: dp[i][0] = dp[i-1][0] + 1 if dp[i-1][1]: dp[i][1] = 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: if not nums: return 0 cur_pos_product_len = 1 if nums[0] > 0 else 0 cur_neg_product_len = 1 if nums[0] < 0 else 0 max_len = cur_pos_product_len for i in range(1, len(nums)): if nums[i] > 0: cur_pos_product_len += 1 cur_neg_product_len = cur_neg_product_len + 1 if cur_neg_product_len > 0 else 0 elif nums[i] < 0: temp = cur_pos_product_len cur_pos_product_len = cur_neg_product_len + 1 if cur_neg_product_len > 0 else 0 cur_neg_product_len = temp + 1 else: cur_pos_product_len = 0 cur_neg_product_len = 0 max_len = max(max_len, cur_pos_product_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: arrs=[[]] for i in nums: if i: arrs[-1].append(i) else: arrs.append([]) ans=0 for arr in arrs: if not len(arr): continue negs=0 for i in arr: if i<0: negs+=1 if negs%2: p1=0 while arr[p1]>0: p1+=1 p2=len(arr)-1 while arr[p2]>0: p2-=1 ans=max(len(arr)-p1-1,p2,ans) else: ans=max(ans,len(arr)) 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_length = 0 neg_length = 0 max_length = 0 for num in nums: if num > 0: pos_length += 1 if neg_length: neg_length += 1 elif num < 0: tmp = pos_length if neg_length: pos_length = neg_length + 1 else: pos_length = 0 neg_length = tmp + 1 else: pos_length = 0 neg_length = 0 max_length = max(max_length, pos_length) # print(f'num, max_length = {num}, {max_length}') max_length = max(max_length, pos_length) 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: n = len(nums) dp = [[0, 0] for _ in range(n)] for i in range(n): if nums[i] == 0: dp[i] = [0, 0] elif i == 0 or nums[i - 1] == 0: if nums[i] > 0: dp[i][0] = 1 else: dp[i][1] = 1 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 return max(dp[i][0] for i in range(n))
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: nums.append(0) left = 0 n = len(nums) first_neg = n last_neg = 0 ct_neg = 0 ans = 0 for right in range(n): if nums[right]<0: first_neg = min(first_neg, right) last_neg = max(last_neg, right) ct_neg += 1 elif nums[right]==0: if left < right: if ct_neg & 1 == 0: ans = max(ans, right-left) else: print(left, right, first_neg, last_neg) ans = max(ans, right-first_neg-1) ans = max(ans, last_neg-left) left = right+1 first_neg = n last_neg = 0 ct_neg = 0 return ans