{"id":7315,"date":"2020-08-29T23:13:01","date_gmt":"2020-08-30T06:13:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7315"},"modified":"2020-08-30T00:13:59","modified_gmt":"2020-08-30T07:13:59","slug":"leetcode-1567-maximum-length-of-subarray-with-positive-product","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1567-maximum-length-of-subarray-with-positive-product\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1567. Maximum Length of Subarray With Positive Product"},"content":{"rendered":"\n<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>\n\n\n\n<p>A subarray of an array is a consecutive sequence of zero or more values taken out of that array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the maximum length of a subarray with positive product<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,-2,-3,4]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>The array nums already has a positive product of 24.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,1,-2,-3,-4]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>The longest subarray with positive product is [1,-2,-3] which has a product of 6.\nNotice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-1,-2,-3,0,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>The longest subarray with positive product is [-1,-2] or [-2,-3].\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-1,2]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,5,-6,4,0,10]\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int getMaxLen(vector<int>& nums) {\n    int p = 0;\n    int n = 0;\n    int ans = 0;\n    for (int x : nums) {      \n      if (x > 0) {\n        ++p;\n        if (n) ++n;\n      } else if (x < 0) {\n        int tp = p;\n        p = n ? n + 1 : 0;\n        n = tp + 1;\n      } else {\n        p = n = 0;        \n      }      \n      ans = max(ans, p);\n    }    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums, find&nbsp;the maximum length of a subarray where the product of all its elements is positive. A subarray of an array&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,645,177],"class_list":["post-7315","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-longest-subarray","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7315"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7315\/revisions"}],"predecessor-version":[{"id":7317,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7315\/revisions\/7317"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}