{"id":7278,"date":"2020-08-22T11:59:41","date_gmt":"2020-08-22T18:59:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7278"},"modified":"2020-08-23T01:11:32","modified_gmt":"2020-08-23T08:11:32","slug":"leetcode-1558-minimum-numbers-of-function-calls-to-make-target-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1558-minimum-numbers-of-function-calls-to-make-target-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array"},"content":{"rendered":"\n<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\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array - \u5237\u9898\u627e\u5de5\u4f5c EP351\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/YQlNemdLtCc?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>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/10\/sample_2_1887.png\" alt=\"\"\/><\/figure>\n\n\n\n<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>\n\n\n\n<p>Return the minimum number of&nbsp;function calls to make&nbsp;<code>nums<\/code>&nbsp;from&nbsp;<code>arr<\/code>.<\/p>\n\n\n\n<p>The answer is guaranteed to fit in a 32-bit signed integer.<\/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 = [1,5]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).\nDouble all the elements: [0, 1] -&gt; [0, 2] -&gt; [0, 4] (2 operations).\nIncrement by 1 (both elements)  [0, 4] -&gt; [1, 4] -&gt; <strong>[1, 5]<\/strong> (2 operations).\nTotal of operations: 1 + 2 + 2 = 5.\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,2]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Increment by 1 (both elements) [0, 0] -&gt; [0, 1] -&gt; [1, 1] (2 operations).\nDouble all the elements: [1, 1] -&gt; <strong>[2, 2]<\/strong> (1 operation).\nTotal of operations: 2 + 1 = 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [4,2,5]\n<strong>Output:<\/strong> 6\n<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).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,2,2,4]\n<strong>Output:<\/strong> 7\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,4,8,16]\n<strong>Output:<\/strong> 8\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;= 10^5<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: count 1s<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(n*log(max(arr_i))<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++\">\nclass Solution {\npublic:\n  int minOperations(vector<int>& nums) {\n    int ans = 0;\n    int high = 0;\n    for (int x : nums) {\n      high = max(high, 31 - __builtin_clz(x | 1));\n      ans += std::bitset<32>(x).count();\n    }\n    return ans + high;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"Java\">\nclass Solution {\n  public int minOperations(int[] nums) {\n    int ans = 0;\n    int high = 0;\n    for (int x : nums) {\n      int l = -1;\n      while (x != 0) {        \n        ans += x & 1;\n        x >>= 1; \n        ++l;\n      }\n      high = Math.max(high, l);\n    }\n    return ans + high;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def minOperations(self, nums: List[int]) -> int:\n    return sum(bin(x).count('1') for x in nums) + len(bin(max(nums))) - 3\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Your task is to form&nbsp;an integer array&nbsp;nums&nbsp;from an initial array of zeros&nbsp;arr&nbsp;that is the&nbsp;same size&nbsp;as&nbsp;nums. Return the minimum number of&nbsp;function calls to make&nbsp;nums&nbsp;from&nbsp;arr. The answer&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[39,16,642,177],"class_list":["post-7278","post","type-post","status-publish","format-standard","hentry","category-bit","tag-binary","tag-bit","tag-highest-bit","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7278","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=7278"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7278\/revisions"}],"predecessor-version":[{"id":7289,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7278\/revisions\/7289"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}