How to find the length of the longest subarray whose product is positive by leetcode
This article mainly introduces how leetcode calculates the length of the longest sub-array whose product is a positive number, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.
Give you an integer array nums, please find out the length of the longest subarray whose product is positive.
A subarray of an array is an array of zero or more consecutive numbers in the original array.
Please return the length of the longest subarray whose product is positive.
Example 1:
Input: nums = [1mai mai 2mi mi 3jue 4]
Output: 4
Explanation: the product of the array itself is a positive number with a value of 24.
Example 2:
Input: nums = [0je 1m m m 2m m 3m m el 4]
Output: 3
Explanation: the subarray whose longest product is positive is [1], and the product is 6.
Note that we cannot include 0 in the subarray, because the product is 0, not a positive number.
Example 3:
Input: nums = [- 1 flint 2meme 3jin0jue 1]
Output: 2
Explanation: the longest subarray whose product is positive is [- 1] or [- 2].
Example 4:
Input: nums = [- 1BI 2]
Output: 1
Example 5:
Input: nums = [1, 2, 3, 5, 5, 6, 4, 0, 10]
Output: 4
Tip:
one