{"id":9601,"date":"2022-04-02T14:30:55","date_gmt":"2022-04-02T21:30:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9601"},"modified":"2022-04-02T14:32:00","modified_gmt":"2022-04-02T21:32:00","slug":"leetcode-2220-minimum-bit-flips-to-convert-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-2220-minimum-bit-flips-to-convert-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2220. Minimum Bit Flips to Convert Number"},"content":{"rendered":"\n<p>A&nbsp;<strong>bit flip<\/strong>&nbsp;of a number&nbsp;<code>x<\/code>&nbsp;is choosing a bit in the binary representation of&nbsp;<code>x<\/code>&nbsp;and&nbsp;<strong>flipping<\/strong>&nbsp;it from either&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>1<\/code>&nbsp;or&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, for&nbsp;<code>x = 7<\/code>, the binary representation is&nbsp;<code>111<\/code>&nbsp;and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get&nbsp;<code>110<\/code>, flip the second bit from the right to get&nbsp;<code>101<\/code>, flip the fifth bit from the right (a leading zero) to get&nbsp;<code>10111<\/code>, etc.<\/li><\/ul>\n\n\n\n<p>Given two integers&nbsp;<code>start<\/code>&nbsp;and&nbsp;<code>goal<\/code>, return<em>&nbsp;the&nbsp;<strong>minimum<\/strong>&nbsp;number of&nbsp;<strong>bit flips<\/strong>&nbsp;to convert&nbsp;<\/em><code>start<\/code><em>&nbsp;to&nbsp;<\/em><code>goal<\/code>.<\/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> start = 10, goal = 7\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:\n- Flip the first bit from the right: 1010 -&gt; 1011.\n- Flip the third bit from the right: 1011 -&gt; 1111.\n- Flip the fourth bit from the right: 1111 -&gt; 0111.\nIt can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.<\/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> start = 3, goal = 4\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:\n- Flip the first bit from the right: 011 -&gt; 010.\n- Flip the second bit from the right: 010 -&gt; 000.\n- Flip the third bit from the right: 000 -&gt; 100.\nIt can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= start, goal &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: XOR<\/strong><\/p>\n\n\n\n<p>start ^ goal will give us the bitwise difference of start and goal in binary format. <br>ans = # of 1 ones in the xor-ed results. <br>For C++, we can use __builtin_popcount or bitset&lt;32>::count() to get the number of bits set for a given integer.<\/p>\n\n\n\n<p>Time complexity: O(1)<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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minBitFlips(int start, int goal) {\n    return bitset<32>(start ^ goal).count(); \/\/ __builtin_popcount(start ^ goal);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;bit flip&nbsp;of a number&nbsp;x&nbsp;is choosing a bit in the binary representation of&nbsp;x&nbsp;and&nbsp;flipping&nbsp;it from either&nbsp;0&nbsp;to&nbsp;1&nbsp;or&nbsp;1&nbsp;to&nbsp;0. For example, for&nbsp;x = 7, the binary representation is&nbsp;111&nbsp;and we may&#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":[16,222,63],"class_list":["post-9601","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-easy","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9601","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=9601"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9601\/revisions"}],"predecessor-version":[{"id":9604,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9601\/revisions\/9604"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}