{"id":7467,"date":"2020-10-04T13:26:05","date_gmt":"2020-10-04T20:26:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7467"},"modified":"2020-10-04T13:26:39","modified_gmt":"2020-10-04T20:26:39","slug":"leetcode-1611-minimum-one-bit-operations-to-make-integers-zero","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1611-minimum-one-bit-operations-to-make-integers-zero\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1611. Minimum One Bit Operations to Make Integers Zero"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, you must transform it into&nbsp;<code>0<\/code>&nbsp;using the following operations any number of times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Change the rightmost (<code>0<sup>th<\/sup><\/code>) bit in the binary representation of&nbsp;<code>n<\/code>.<\/li><li>Change the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;bit in the binary representation of&nbsp;<code>n<\/code>&nbsp;if the&nbsp;<code>(i-1)<sup>th<\/sup><\/code>&nbsp;bit is set to&nbsp;<code>1<\/code>&nbsp;and the&nbsp;<code>(i-2)<sup>th<\/sup><\/code>&nbsp;through&nbsp;<code>0<sup>th<\/sup><\/code>&nbsp;bits are set to&nbsp;<code>0<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the minimum number of operations to transform&nbsp;<\/em><code>n<\/code><em>&nbsp;into&nbsp;<\/em><code>0<\/code><em>.<\/em><\/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> n = 0\n<strong>Output:<\/strong> 0\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> n = 3\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The binary representation of 3 is \"11\".\n\"11\" -&gt; \"01\" with the 2nd operation since the 0th bit is 1.\n\"01\" -&gt; \"00\" with the 1st operation.\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> n = 6\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The binary representation of 6 is \"110\".\n\"110\" -&gt; \"010\" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.\n\"010\" -&gt; \"011\" with the 1st operation.\n\"011\" -&gt; \"001\" with the 2nd operation since the 0th bit is 1.\n\"001\" -&gt; \"000\" with the 1st operation.\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> n = 9\n<strong>Output:<\/strong> 14\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> n = 333\n<strong>Output:<\/strong> 393\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= n &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Graycode<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/p>\n\n\n\n<p>Ans is the order of n in graycode.<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minimumOneBitOperations(int n) {\n    int ans = 0;\n    while (n) {\n      ans ^= n;\n      n >>= 1;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, you must transform it into&nbsp;0&nbsp;using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of&nbsp;n.&#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,49],"tags":[16,658,217,31],"class_list":["post-7467","post","type-post","status-publish","format-standard","hentry","category-bit","category-math","tag-bit","tag-graycode","tag-hard","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7467","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=7467"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7467\/revisions"}],"predecessor-version":[{"id":7469,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7467\/revisions\/7469"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}