{"id":3594,"date":"2018-08-17T22:02:31","date_gmt":"2018-08-18T05:02:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3594"},"modified":"2018-08-17T22:23:18","modified_gmt":"2018-08-18T05:23:18","slug":"leetcode-628-maximum-product-of-three-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-628-maximum-product-of-three-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 628. Maximum Product of Three Numbers"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 628. Maximum Product of Three Numbers - \u5237\u9898\u627e\u5de5\u4f5c EP15\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/_ID-lcUd_vg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1>Problem<\/h1>\n<p>Given an integer array, find three numbers whose product is maximum and output the maximum product.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [1,2,3]\r\n<b>Output:<\/b> 6\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> [1,2,3,4]\r\n<b>Output:<\/b> 24\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of the given array will be in range [3,10<sup>4<\/sup>] and all elements are in the range [-1000, 1000].<\/li>\n<li>Multiplication of any three numbers in the input won&#8217;t exceed the range of 32-bit signed integer.<\/li>\n<\/ol>\n<h1>Idea:<\/h1>\n<p>Find the top 3 numbers t1, t2, t3, and bottom 2 numbers, b1, b2.<\/p>\n<p>If all numbers are positives,\u00a0 answer must be t1 * t2 * t3.<\/p>\n<p>Since the number can go negative, the answer must be either t1*t2*t3 or b1 * b2 * t1, if b1 and b2 are both negatives.<\/p>\n<p>ex. nums: [5, 1, -6, 3, -1]<\/p>\n<p>t1, t2, t3: 5, 3, 1<\/p>\n<p>b1, b2: -6, -1<\/p>\n<p>t1 * t2 * t3 = 15<\/p>\n<p>t1 * b1 * b2 = 30<\/p>\n<h1><strong>Solution 1: Manual Tracking<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 28 ms\r\nclass Solution {\r\npublic:\r\n  int maximumProduct(vector&lt;int&gt;&amp; nums) {\r\n    int max1 = INT_MIN;\r\n    int max2 = INT_MIN;\r\n    int max3 = INT_MIN;\r\n    int min1 = INT_MAX;\r\n    int min2 = INT_MAX;\r\n\r\n    for (const int num: nums) {\r\n      if (num &gt; max1) {\r\n          max3 = max2;\r\n          max2 = max1;\r\n          max1 = num;\r\n      } else if (num &gt; max2) {\r\n          max3 = max2;\r\n          max2 = num;\r\n      } else if (num &gt; max3) {\r\n          max3=num;\r\n      }\r\n\r\n      if (num &lt; min1) {\r\n          min2 = min1;\r\n          min1 = num;\r\n      } else if (num &lt; min2) {\r\n          min2=num;\r\n      }\r\n    }\r\n\r\n    return max(max1*max2*max3, max1*min1*min2);\r\n  }\r\n};<\/pre>\n<h1><strong>Solution 2: Sorting<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 48 ms\r\nclass Solution {\r\npublic:\r\n  int maximumProduct(vector&lt;int&gt;&amp; nums) {\r\n    int n = nums.size();\r\n    sort(nums.rbegin(), nums.rend());\r\n    return max(nums[0] * nums[1] * nums[2], nums[0] * nums[n - 1] * nums[n - 2]);\r\n  }\r\n};<\/pre>\n<h1><strong>Solution 3: Two Heaps (Priority Queues)<\/strong><\/h1>\n<p>Time complexity: O(nlog3)<\/p>\n<p>Space complexity: O(2 + 3)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 40 ms\r\nclass Solution {\r\npublic:\r\n  int maximumProduct(vector&lt;int&gt;&amp; nums) {\r\n    priority_queue&lt;int, vector&lt;int&gt;, less&lt;int&gt;&gt; min_q; \/\/ max_heap\r\n    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; max_q; \/\/ min_heap\r\n    \r\n    for (int num : nums) {\r\n      max_q.push(num);\r\n      if (max_q.size() &gt; 3) max_q.pop();\r\n      min_q.push(num);\r\n      if (min_q.size() &gt; 2) min_q.pop();\r\n    }\r\n    \r\n    int max3 = max_q.top(); max_q.pop();\r\n    int max2 = max_q.top(); max_q.pop();\r\n    int max1 = max_q.top(); max_q.pop();\r\n    int min2 = min_q.top(); min_q.pop();\r\n    int min1 = min_q.top(); min_q.pop();\r\n    \r\n    return max(max1 * max2 * max3, max1 * min1 * min2); \r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,73,31,141,382,23,381],"class_list":["post-3594","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-heap","tag-math","tag-max","tag-priority_queue","tag-sort","tag-topk","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3594","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=3594"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3594\/revisions"}],"predecessor-version":[{"id":3600,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3594\/revisions\/3600"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}