{"id":8914,"date":"2021-11-28T22:22:18","date_gmt":"2021-11-29T06:22:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8914"},"modified":"2021-11-28T22:36:05","modified_gmt":"2021-11-29T06:36:05","slug":"leetcode-152-maximum-product-subarray","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-152-maximum-product-subarray\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 152. Maximum Product Subarray"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>, find a contiguous non-empty subarray within the array that has the largest product, and return&nbsp;<em>the product<\/em>.<\/p>\n\n\n\n<p>It is&nbsp;<strong>guaranteed<\/strong>&nbsp;that the answer will fit in a&nbsp;<strong>32-bit<\/strong>&nbsp;integer.<\/p>\n\n\n\n<p>A&nbsp;<strong>subarray<\/strong>&nbsp;is a contiguous subsequence of the array.<\/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 = [2,3,-2,4]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> [2,3] has the largest product 6.\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 = [-2,0,-1]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The result cannot be 2, because [-2,-1] is not a subarray.\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;= 2 * 10<sup>4<\/sup><\/code><\/li><li><code>-10 &lt;= nums[i] &lt;= 10<\/code><\/li><li>The product of any prefix or suffix of&nbsp;<code>nums<\/code>&nbsp;is&nbsp;<strong>guaranteed<\/strong>&nbsp;to fit in a&nbsp;<strong>32-bit<\/strong>&nbsp;integer.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Track high and low<\/strong><\/h2>\n\n\n\n<p>Compute the low \/ high of prefix product, reset if nums[i] is higher than high or lower than low.<\/p>\n\n\n\n<p>Swap low and high if nums[i] is a negative number.<\/p>\n\n\n\n<p>e.g. [2, 3, -1, 8, -2]<br>nums[i] = 2, low = 2, high = 2<br>nums[i] = 3, low = 3, high = 2 * 3 = 6<br>nums[i] = -1, low = 6 * -1 = -6, high = -1<br>nums[i] = 8, low = -6 * 8 = -48, high = 8<br>nums[i] = -2, low = 8*-2 = -16, high = -48 * -2 = <strong><span class=\"has-inline-color has-vivid-red-color\">96<\/span><\/strong><\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: 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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxProduct(vector<int>& nums) {   \n    int ans = nums[0];\n    for (int i = 1, h = ans, l = ans; i < nums.size(); ++i) {\n      if (nums[i] < 0) swap(l, h);\n      l = min(nums[i], nums[i] * l);\n      h = max(nums[i], nums[i] * h);\n      ans = max(ans, h);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums, find a contiguous non-empty subarray within the array that has the largest product, and return&nbsp;the product. It is&nbsp;guaranteed&nbsp;that the answer will&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[177,41],"class_list":["post-8914","post","type-post","status-publish","format-standard","hentry","category-array","tag-medium","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8914","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=8914"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8914\/revisions"}],"predecessor-version":[{"id":8921,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8914\/revisions\/8921"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}