LeetCode 1567. Maximum Length of Subarray With Positive Product Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
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: Dynamic Programming

We define two arrays f and g of length n, where f[i] represents the length of the longest subarray ending at nums[i] with a positive product, and g[i] represents the length of the longest subarray ending at nums[i] with a negative product.

Initially, if nums[0] > 0, then f[0] = 1, otherwise f[0] = 0; if nums[0] < 0, then g[0] = 1, otherwise g[0] = 0. We initialize the answer ans = f[0].

Next, we iterate through the array nums starting from i = 1. For each i, we have the following cases:

  • If nums[i] > 0, then f[i] can be transferred from f[i - 1], i.e., f[i] = f[i - 1] + 1, and the value of g[i] depends on whether g[i - 1] is 0. If g[i - 1] = 0, then g[i] = 0, otherwise g[i] = g[i - 1] + 1;
  • If nums[i] < 0, then the value of f[i] depends on whether g[i - 1] is 0. If g[i - 1] = 0, then f[i] = 0, otherwise f[i] = g[i - 1] + 1, and g[i] can be transferred from f[i - 1], i.e., g[i] = f[i - 1] + 1.
  • Then, we update the answer ans = max(ans, f[i]).

After the iteration, we return the answer ans.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScript
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) f = [0] * n g = [0] * n f[0] = int(nums[0] > 0) g[0] = int(nums[0] < 0) ans = f[0] for i in range(1, n): if nums[i] > 0: f[i] = f[i - 1] + 1 g[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 elif nums[i] < 0: f[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1 g[i] = f[i - 1] + 1 ans = max(ans, f[i]) return ans(code-box)

Solution 2: Dynamic Programming (Space Optimization)

We observe that for each i, the values of f[i] and g[i] only depend on f[i - 1] and g[i - 1]. Therefore, we can use two variables f and g to record the values of f[i - 1] and g[i - 1], respectively, thus optimizing the space complexity to O(1).

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

PythonJavaC++GoTypeScript
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) f = int(nums[0] > 0) g = int(nums[0] < 0) ans = f for i in range(1, n): ff = gg = 0 if nums[i] > 0: ff = f + 1 gg = 0 if g == 0 else g + 1 elif nums[i] < 0: ff = 0 if g == 0 else g + 1 gg = f + 1 f, g = ff, gg ans = max(ans, f) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !