<?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>highest bit Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/highest-bit/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/highest-bit/</link>
	<description></description>
	<lastBuildDate>Sun, 23 Aug 2020 08:11:32 +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>highest bit Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/highest-bit/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1558-minimum-numbers-of-function-calls-to-make-target-array/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1558-minimum-numbers-of-function-calls-to-make-target-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 22 Aug 2020 18:59:41 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[highest bit]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7278</guid>

					<description><![CDATA[<p>Your task is to form&#160;an integer array&#160;nums&#160;from an initial array of zeros&#160;arr&#160;that is the&#160;same size&#160;as&#160;nums. Return the minimum number of&#160;function calls to make&#160;nums&#160;from&#160;arr. The answer&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1558-minimum-numbers-of-function-calls-to-make-target-array/">花花酱 LeetCode 1558. Minimum Numbers of Function Calls to Make Target 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 1558. Minimum Numbers of Function Calls to Make Target Array - 刷题找工作 EP351" width="500" height="281" src="https://www.youtube.com/embed/YQlNemdLtCc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/07/10/sample_2_1887.png" alt=""/></figure>



<p>Your task is to form&nbsp;an integer array&nbsp;<code>nums</code>&nbsp;from an initial array of zeros&nbsp;<code>arr</code>&nbsp;that is the&nbsp;same size&nbsp;as&nbsp;<code>nums</code>.</p>



<p>Return the minimum number of&nbsp;function calls to make&nbsp;<code>nums</code>&nbsp;from&nbsp;<code>arr</code>.</p>



<p>The answer is guaranteed to fit in a 32-bit signed integer.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,5]
<strong>Output:</strong> 5
<strong>Explanation:</strong> Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -&gt; [0, 2] -&gt; [0, 4] (2 operations).
Increment by 1 (both elements)  [0, 4] -&gt; [1, 4] -&gt; <strong>[1, 5]</strong> (2 operations).
Total of operations: 1 + 2 + 2 = 5.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Increment by 1 (both elements) [0, 0] -&gt; [0, 1] -&gt; [1, 1] (2 operations).
Double all the elements: [1, 1] -&gt; <strong>[2, 2]</strong> (1 operation).
Total of operations: 2 + 1 = 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,2,5]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (initial)[0,0,0] -&gt; [1,0,0] -&gt; [1,0,1] -&gt; [2,0,2] -&gt; [2,1,2] -&gt; [4,2,4] -&gt; <strong>[4,2,5]</strong>(nums).
</pre>



<p><strong>Example 4:</strong></p>



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



<p><strong>Example 5:</strong></p>



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



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



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



<h2><strong>Solution: count 1s</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/08/1558-ep351.png" alt="" class="wp-image-7288" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/08/1558-ep351.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/08/1558-ep351-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/08/1558-ep351-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/08/1558-ep351-2.png" alt="" class="wp-image-7287" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/08/1558-ep351-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/08/1558-ep351-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/08/1558-ep351-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>For 5 (101b), we can add 1s for 5 times which of cause isn&#8217;t the best way to generate 5, the optimal way is to [+1, *2, +1]. We have to add 1 for each 1 in the binary format. e.g. 11 (1011), we need 3x &#8220;+1&#8221; op, and 4 &#8220;*2&#8221; op.  Fortunately, the &#8220;*2&#8221; can be shared/delayed,  thus we just need to find the largest number.<br>e.g. [2,4,8,16]<br>[0, 0, 0, 0] -&gt; [0, 0, 0, 1] -&gt; [0, 0, 0, 2]<br>[0, 0, 0, 2] -&gt; [0, 0, 1, 2] -&gt; [0, 0, 2, 4]<br>[0, 0, 2, 4] -&gt; [0, 1, 2, 4] -&gt; [0, 2, 4, 8]<br>[0, 2, 4, 8] -&gt; [1, 2, 4, 8] -&gt; [2, 4, 8, 16]<br>ans = sum{count_1(arr_i)} + high_bit(max(arr_i))</p>



<p>Time complexity: O(n*log(max(arr_i))<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int minOperations(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    int high = 0;
    for (int x : nums) {
      high = max(high, 31 - __builtin_clz(x | 1));
      ans += std::bitset&lt;32&gt;(x).count();
    }
    return ans + high;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
  public int minOperations(int[] nums) {
    int ans = 0;
    int high = 0;
    for (int x : nums) {
      int l = -1;
      while (x != 0) {        
        ans += x &amp; 1;
        x &gt;&gt;= 1; 
        ++l;
      }
      high = Math.max(high, l);
    }
    return ans + high;
  }
}</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def minOperations(self, nums: List[int]) -&gt; int:
    return sum(bin(x).count('1') for x in nums) + len(bin(max(nums))) - 3</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1558-minimum-numbers-of-function-calls-to-make-target-array/">花花酱 LeetCode 1558. Minimum Numbers of Function Calls to Make Target 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/bit/leetcode-1558-minimum-numbers-of-function-calls-to-make-target-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
