{"id":6451,"date":"2020-03-09T23:20:49","date_gmt":"2020-03-10T06:20:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6451"},"modified":"2020-03-09T23:21:39","modified_gmt":"2020-03-10T06:21:39","slug":"leetcode-672-bulb-switcher-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-672-bulb-switcher-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 672. Bulb Switcher II"},"content":{"rendered":"\n<p>There is a room with&nbsp;<code>n<\/code>&nbsp;lights which are turned on initially and 4 buttons on the wall. After performing exactly&nbsp;<code>m<\/code>&nbsp;unknown operations towards buttons, you need to return how many different kinds of status of the&nbsp;<code>n<\/code>&nbsp;lights could be.<\/p>\n\n\n\n<p>Suppose&nbsp;<code>n<\/code>&nbsp;lights are labeled as number [1, 2, 3 &#8230;, n], function of these 4 buttons are given below:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Flip all the lights.<\/li><li>Flip lights with even numbers.<\/li><li>Flip lights with odd numbers.<\/li><li>Flip lights with (3k + 1) numbers, k = 0, 1, 2, &#8230;<\/li><\/ol>\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 = 1, m = 1.\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Status can be: [on], [off]\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 = 2, m = 1.\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Status can be: [on, off], [off, on], [off, off]\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 = 3, m = 1.\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>m<\/code>&nbsp;both fit in range [0, 1000].<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution1: Bitmask + Simulation<\/strong><\/h2>\n\n\n\n<p>The light pattern will be repeated if we have more than 6 lights, so n = n % 6, n = 6 if n == 0.<\/p>\n\n\n\n<p>Time complexity: O(m*2^6)<br>Space complexity: O(2^6)<\/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 flipLights(int n, int m) {\n    if (n > 6) n = n % 6 + 6;\n    unordered_set<int> s1{(1 << n) - 1};\n    auto flip = [n](int s, int start, int step) {\n      for (int i = start; i < n; i += step)\n        s ^= (1 << i);\n      return s;\n    };\n    for (int i = 0; i < m; ++i) {\n      unordered_set<int> s2;\n      for (int s : s1) {\n        s2.insert(flip(s, 0, 1));\n        s2.insert(flip(s, 0, 2));\n        s2.insert(flip(s, 1, 2));\n        s2.insert(flip(s, 0, 3));\n      }\n      s1.swap(s2);\n    }\n    return s1.size();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a room with&nbsp;n&nbsp;lights which are turned on initially and 4 buttons on the wall. After performing exactly&nbsp;m&nbsp;unknown operations towards buttons, you need to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[31,177,179],"class_list":["post-6451","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-math","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6451","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=6451"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6451\/revisions"}],"predecessor-version":[{"id":6453,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6451\/revisions\/6453"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}