<?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>topk Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/topk/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/topk/</link>
	<description></description>
	<lastBuildDate>Sat, 18 Aug 2018 05:23:18 +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>topk Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/topk/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 628. Maximum Product of Three Numbers</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-628-maximum-product-of-three-numbers/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-628-maximum-product-of-three-numbers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 18 Aug 2018 05:02:31 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[topk]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3594</guid>

					<description><![CDATA[<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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-628-maximum-product-of-three-numbers/">花花酱 LeetCode 628. Maximum Product of Three Numbers</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><iframe width="500" height="375" src="https://www.youtube.com/embed/_ID-lcUd_vg?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1>Problem</h1>
<p>Given an integer array, find three numbers whose product is maximum and output the maximum product.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> [1,2,3]
<b>Output:</b> 6
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false "><b>Input:</b> [1,2,3,4]
<b>Output:</b> 24
</pre>
<p><b>Note:</b></p>
<ol>
<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>
<li>Multiplication of any three numbers in the input won&#8217;t exceed the range of 32-bit signed integer.</li>
</ol>
<h1>Idea:</h1>
<p>Find the top 3 numbers t1, t2, t3, and bottom 2 numbers, b1, b2.</p>
<p>If all numbers are positives,  answer must be t1 * t2 * t3.</p>
<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>
<p>ex. nums: [5, 1, -6, 3, -1]</p>
<p>t1, t2, t3: 5, 3, 1</p>
<p>b1, b2: -6, -1</p>
<p>t1 * t2 * t3 = 15</p>
<p>t1 * b1 * b2 = 30</p>
<h1><strong>Solution 1: Manual Tracking</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 28 ms
class Solution {
public:
  int maximumProduct(vector&lt;int&gt;&amp; nums) {
    int max1 = INT_MIN;
    int max2 = INT_MIN;
    int max3 = INT_MIN;
    int min1 = INT_MAX;
    int min2 = INT_MAX;

    for (const int num: nums) {
      if (num &gt; max1) {
          max3 = max2;
          max2 = max1;
          max1 = num;
      } else if (num &gt; max2) {
          max3 = max2;
          max2 = num;
      } else if (num &gt; max3) {
          max3=num;
      }

      if (num &lt; min1) {
          min2 = min1;
          min1 = num;
      } else if (num &lt; min2) {
          min2=num;
      }
    }

    return max(max1*max2*max3, max1*min1*min2);
  }
};</pre><p></p>
<h1><strong>Solution 2: Sorting</strong></h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 48 ms
class Solution {
public:
  int maximumProduct(vector&lt;int&gt;&amp; nums) {
    int n = nums.size();
    sort(nums.rbegin(), nums.rend());
    return max(nums[0] * nums[1] * nums[2], nums[0] * nums[n - 1] * nums[n - 2]);
  }
};</pre><p></p>
<h1><strong>Solution 3: Two Heaps (Priority Queues)</strong></h1>
<p>Time complexity: O(nlog3)</p>
<p>Space complexity: O(2 + 3)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 40 ms
class Solution {
public:
  int maximumProduct(vector&lt;int&gt;&amp; nums) {
    priority_queue&lt;int, vector&lt;int&gt;, less&lt;int&gt;&gt; min_q; // max_heap
    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; max_q; // min_heap
    
    for (int num : nums) {
      max_q.push(num);
      if (max_q.size() &gt; 3) max_q.pop();
      min_q.push(num);
      if (min_q.size() &gt; 2) min_q.pop();
    }
    
    int max3 = max_q.top(); max_q.pop();
    int max2 = max_q.top(); max_q.pop();
    int max1 = max_q.top(); max_q.pop();
    int min2 = min_q.top(); min_q.pop();
    int min1 = min_q.top(); min_q.pop();
    
    return max(max1 * max2 * max3, max1 * min1 * min2); 
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-628-maximum-product-of-three-numbers/">花花酱 LeetCode 628. Maximum Product of Three Numbers</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/math/leetcode-628-maximum-product-of-three-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
