<?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>operation Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/operation/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/operation/</link>
	<description></description>
	<lastBuildDate>Mon, 30 Nov 2020 22:04:34 +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>operation Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/operation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1675. Minimize Deviation in Array</title>
		<link>https://zxi.mytechroad.com/blog/priority-queue/leetcode-1675-minimize-deviation-in-array/</link>
					<comments>https://zxi.mytechroad.com/blog/priority-queue/leetcode-1675-minimize-deviation-in-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Nov 2020 21:54:59 +0000</pubDate>
				<category><![CDATA[Priority Queue]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[operation]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[treeset]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7743</guid>

					<description><![CDATA[<p>You are given an array&#160;nums&#160;of&#160;n&#160;positive integers. You can perform two types of operations on any element of the array any number of times: If the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-1675-minimize-deviation-in-array/">花花酱 LeetCode 1675. Minimize Deviation in Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1675. Minimize Deviation in Array - 刷题找工作 EP372" width="500" height="281" src="https://www.youtube.com/embed/l_o4fp6BHYY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an array&nbsp;<code>nums</code>&nbsp;of&nbsp;<code>n</code>&nbsp;positive integers.</p>



<p>You can perform two types of operations on any element of the array any number of times:</p>



<ul><li>If the element is&nbsp;<strong>even</strong>,&nbsp;<strong>divide</strong>&nbsp;it by&nbsp;<code>2</code>.<ul><li>For example, if the array is&nbsp;<code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be&nbsp;<code>[1,2,3,2].</code></li></ul></li><li>If the element is&nbsp;<strong>odd</strong>,&nbsp;<strong>multiply</strong>&nbsp;it by&nbsp;<code>2</code>.<ul><li>For example, if the array is&nbsp;<code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be&nbsp;<code>[2,2,3,4].</code></li></ul></li></ul>



<p>The&nbsp;<strong>deviation</strong>&nbsp;of the array is the&nbsp;<strong>maximum difference</strong>&nbsp;between any two elements in the array.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum deviation</strong>&nbsp;the array can have after performing some number of operations.</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> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,2], then to [2,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,2,5,5,3], then the deviation will be 5 - 2 = 3.
</pre>



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



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



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



<ul><li><code>n == nums.length</code></li><li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Priority Queue</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-1.png" alt="" class="wp-image-7755" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-2.png" alt="" class="wp-image-7756" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1675-ep372-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>If we double an odd number it becomes an even number, then we can only divide it by two which gives us back the original number. So we can pre-double all the odd numbers and only do division in the following process. </p>



<p>We push all numbers including pre-doubled odd ones onto a priority queue, and track the difference between the largest and smallest number.</p>



<p>Each time, we pop the largest number out and divide it by two then put it back to the priority queue, until the largest number becomes odd. We can not discard it and divide any other smaller numbers by two will only increase the max difference, so we can stop here.</p>



<p>ex1: [3, 5, 8] =&gt; [6, 8, 10] (pre-double) =&gt; [5, 6, 8] =&gt; [4, 5, 6] =&gt; [3, 4, 5] max diff is 5 &#8211; 3 = 2<br>ex2: [4,1,5,20,3] =&gt; [2, 4, 6, 10, 20] (pre-double) =&gt; [2, 4, 6, 10] =&gt; [2, 4, 5, 6] =&gt; [2,3,4,5] max diff = 5-2 = 3</p>



<p>Time complexity: O(n*logm*logn)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int minimumDeviation(vector&lt;int&gt;&amp; nums) {
    set&lt;int&gt; s;
    for (int x : nums)
      s.insert(x &amp; 1 ? x * 2 : x);
    int ans = *rbegin(s) - *begin(s);
    while (*rbegin(s) % 2 == 0) {
      s.insert(*rbegin(s) / 2);
      s.erase(*rbegin(s));
      ans = min(ans, *rbegin(s) - *begin(s));
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">C++/PQ</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int minimumDeviation(vector&lt;int&gt;&amp; nums) {
    priority_queue&lt;int&gt; q;    
    int lo = INT_MAX;
    for (int x : nums) {
      x = x &amp; 1 ? x * 2 : x;
      q.push(x);
      lo = min(lo, x);
    }
    int ans = q.top() - lo;
    while (q.top() % 2 == 0) {
      int x = q.top(); q.pop();
      q.push(x / 2);
      lo = min(lo, x / 2);
      ans = min(ans, q.top() - lo);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-1675-minimize-deviation-in-array/">花花酱 LeetCode 1675. Minimize Deviation in Array</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/priority-queue/leetcode-1675-minimize-deviation-in-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
