{"id":7166,"date":"2020-07-26T11:52:08","date_gmt":"2020-07-26T18:52:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7166"},"modified":"2020-07-26T11:53:20","modified_gmt":"2020-07-26T18:53:20","slug":"leetcode-1529-bulb-switcher-iv","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1529-bulb-switcher-iv\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1529. Bulb Switcher IV"},"content":{"rendered":"\n<p>There is a room with&nbsp;<code>n<\/code>&nbsp;bulbs, numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>,&nbsp;arranged in a row from left to right. Initially all the bulbs are&nbsp;<strong>turned off<\/strong>.<\/p>\n\n\n\n<p>Your task is to obtain the configuration represented by&nbsp;<code>target<\/code>&nbsp;where&nbsp;<code>target[i]<\/code>&nbsp;is &#8216;1&#8217; if the i-th bulb is turned on and is &#8216;0&#8217; if it is turned off.<\/p>\n\n\n\n<p>You have a switch&nbsp;to flip the state of the bulb,&nbsp;a flip operation is defined as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choose&nbsp;<strong>any<\/strong>&nbsp;bulb (index&nbsp;<code>i<\/code>)&nbsp;of your current configuration.<\/li><li>Flip each bulb from index&nbsp;<code>i<\/code>&nbsp;to&nbsp;<code>n-1<\/code>.<\/li><\/ul>\n\n\n\n<p>When any bulb is flipped it means that if it is 0 it changes to 1 and if it is 1 it changes to 0.<\/p>\n\n\n\n<p>Return the&nbsp;<strong>minimum<\/strong>&nbsp;number of flips required to form&nbsp;<code>target<\/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> target = \"10111\"\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>Initial configuration \"00000\".\nflip from the third bulb:  \"00000\" -&gt; \"00111\"\nflip from the first bulb:  \"00111\" -&gt; \"11000\"\nflip from the second bulb:  \"11000\" -&gt; \"10111\"\nWe need at least 3 flip operations to form target.<\/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> target = \"101\"\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>\"000\" -&gt; \"111\" -&gt; \"100\" -&gt; \"101\".\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> target = \"00000\"\n<strong>Output:<\/strong> 0\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> target = \"001011101\"\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= target.length &lt;= 10^5<\/code><\/li><li><code>target[i] == '0'<\/code>&nbsp;or&nbsp;<code>target[i] == '1'<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: XOR<\/strong><\/h2>\n\n\n\n<p>Flip from left to right, since flipping the a bulb won&#8217;t affect anything in the left.<br>We count how many times flipped so far, and that % 2 will be the state for all the bulb to the right.<br>If the current bulb&#8217;s state != target, we have to flip once.<\/p>\n\n\n\n<p>Time complexity: O(n)<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++\">class Solution {\npublic:\n  int minFlips(string target) {\n    int ans = 0;\n    int cur = 0;\n    for (char c : target) {\n      if (c - '0' != cur) {\n        cur ^= 1;\n        ++ans;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a room with&nbsp;n&nbsp;bulbs, numbered from&nbsp;0&nbsp;to&nbsp;n-1,&nbsp;arranged in a row from left to right. Initially all the bulbs are&nbsp;turned off. Your task is to obtain&#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,302,63],"class_list":["post-7166","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-flip","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7166","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=7166"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7166\/revisions"}],"predecessor-version":[{"id":7168,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7166\/revisions\/7168"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}