<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>longest subarray Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/longest-subarray/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/longest-subarray/</link>
	<description></description>
	<lastBuildDate>Sun, 30 Aug 2020 07:13:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>longest subarray Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/longest-subarray/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1567. Maximum Length of Subarray With Positive Product</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1567-maximum-length-of-subarray-with-positive-product/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1567-maximum-length-of-subarray-with-positive-product/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Aug 2020 06:13:01 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[longest subarray]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7315</guid>

					<description><![CDATA[<p>Given an array of integers&#160;nums, find&#160;the maximum length of a subarray where the product of all its elements is positive. A subarray of an array&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1567-maximum-length-of-subarray-with-positive-product/">花花酱 LeetCode 1567. Maximum Length of Subarray With Positive Product</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an array of integers&nbsp;<code>nums, find</code>&nbsp;the maximum length of a subarray where the product of all its elements is positive.</p>



<p>A subarray of an array is a consecutive sequence of zero or more values taken out of that array.</p>



<p>Return&nbsp;<em>the maximum length of a subarray with positive product</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,-2,-3,4]
<strong>Output:</strong> 4
<strong>Explanation: </strong>The array nums already has a positive product of 24.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,1,-2,-3,-4]
<strong>Output:</strong> 3
<strong>Explanation: </strong>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.</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,-2,-3,0,1]
<strong>Output:</strong> 2
<strong>Explanation: </strong>The longest subarray with positive product is [-1,-2] or [-2,-3].
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,2]
<strong>Output:</strong> 1
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,5,-6,4,0,10]
<strong>Output:</strong> 4
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 10^5</code></li><li><code>-10^9 &lt;= nums[i]&nbsp;&lt;= 10^9</code></li></ul>



<h2><strong>Solution: DP</strong></h2>



<p>p[i] := max length of positive products ends with arr[i]<br>n[i] := max length of negtive products ends with arr[i]</p>



<p>if arr[i] &gt; 0: p[i] = p[i &#8211; 1] + 1, n[i] = n[i] + 1 if n[i] else 0<br>if arr[i] &lt; 0: p[i] = n[i &#8211; 1] + 1 if n[i &#8211; 1] else 0, n[i] = p[i &#8211; 1] + 1<br>if arr[i] == 0: p[i] =  n[i] = 0<br>ans = max(p[i])</p>



<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int getMaxLen(vector&lt;int&gt;&amp; nums) {
    int p = 0;
    int n = 0;
    int ans = 0;
    for (int x : nums) {      
      if (x &gt; 0) {
        ++p;
        if (n) ++n;
      } else if (x &lt; 0) {
        int tp = p;
        p = n ? n + 1 : 0;
        n = tp + 1;
      } else {
        p = n = 0;        
      }      
      ans = max(ans, p);
    }    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1567-maximum-length-of-subarray-with-positive-product/">花花酱 LeetCode 1567. Maximum Length of Subarray With Positive Product</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1567-maximum-length-of-subarray-with-positive-product/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
