返回介绍

solution / 1500-1599 / 1567.Maximum Length of Subarray With Positive Product / README_EN

发布于 2024-06-17 01:03:18 字数 4742 浏览 0 评论 0 收藏 0

1567. Maximum Length of Subarray With Positive Product

中文文档

Description

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].

 

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def getMaxLen(self, nums: List[int]) -> int:
    f1 = 1 if nums[0] > 0 else 0
    f2 = 1 if nums[0] < 0 else 0
    res = f1
    for num in nums[1:]:
      pf1, pf2 = f1, f2
      if num > 0:
        f1 += 1
        if f2 > 0:
          f2 += 1
        else:
          f2 = 0
      elif num < 0:
        pf1, pf2 = f1, f2
        f2 = pf1 + 1
        if pf2 > 0:
          f1 = pf2 + 1
        else:
          f1 = 0
      else:
        f1 = 0
        f2 = 0
      res = max(res, f1)
    return res
class Solution {
  public int getMaxLen(int[] nums) {
    int f1 = nums[0] > 0 ? 1 : 0;
    int f2 = nums[0] < 0 ? 1 : 0;
    int res = f1;
    for (int i = 1; i < nums.length; ++i) {
      if (nums[i] > 0) {
        ++f1;
        f2 = f2 > 0 ? f2 + 1 : 0;
      } else if (nums[i] < 0) {
        int pf1 = f1, pf2 = f2;
        f2 = pf1 + 1;
        f1 = pf2 > 0 ? pf2 + 1 : 0;
      } else {
        f1 = 0;
        f2 = 0;
      }
      res = Math.max(res, f1);
    }
    return res;
  }
}
class Solution {
public:
  int getMaxLen(vector<int>& nums) {
    int f1 = nums[0] > 0 ? 1 : 0;
    int f2 = nums[0] < 0 ? 1 : 0;
    int res = f1;
    for (int i = 1; i < nums.size(); ++i) {
      if (nums[i] > 0) {
        ++f1;
        f2 = f2 > 0 ? f2 + 1 : 0;
      } else if (nums[i] < 0) {
        int pf1 = f1, pf2 = f2;
        f2 = pf1 + 1;
        f1 = pf2 > 0 ? pf2 + 1 : 0;
      } else {
        f1 = 0;
        f2 = 0;
      }
      res = max(res, f1);
    }
    return res;
  }
};
func getMaxLen(nums []int) int {
  f1, f2 := 0, 0
  if nums[0] > 0 {
    f1 = 1
  }
  if nums[0] < 0 {
    f2 = 1
  }
  res := f1
  for i := 1; i < len(nums); i++ {
    if nums[i] > 0 {
      f1++
      if f2 > 0 {
        f2++
      } else {
        f2 = 0
      }
    } else if nums[i] < 0 {
      pf1, pf2 := f1, f2
      f2 = pf1 + 1
      if pf2 > 0 {
        f1 = pf2 + 1
      } else {
        f1 = 0
      }
    } else {
      f1, f2 = 0, 0
    }
    res = max(res, f1)
  }
  return res
}
function getMaxLen(nums: number[]): number {
  // 连续正数计数n1, 连续负数计数n2
  let n1 = nums[0] > 0 ? 1 : 0,
    n2 = nums[0] < 0 ? 1 : 0;
  let ans = n1;
  for (let i = 1; i < nums.length; ++i) {
    let cur = nums[i];
    if (cur == 0) {
      (n1 = 0), (n2 = 0);
    } else if (cur > 0) {
      ++n1;
      n2 = n2 > 0 ? n2 + 1 : 0;
    } else {
      let t1 = n1,
        t2 = n2;
      n1 = t2 > 0 ? t2 + 1 : 0;
      n2 = t1 + 1;
    }
    ans = Math.max(ans, n1);
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文