{"id":1974,"date":"2016-04-15T21:55:38","date_gmt":"2016-04-15T13:55:38","guid":{"rendered":"http:\/\/boweihe.me\/?p=1974"},"modified":"2016-04-15T21:55:38","modified_gmt":"2016-04-15T13:55:38","slug":"leetcode-319-bulb-switcher-%e6%99%ba%e5%95%86%e7%a2%be%e5%8e%8b%e9%a2%98","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1974","title":{"rendered":"LeetCode 319. Bulb Switcher \u667a\u5546\u78be\u538b\u9898"},"content":{"rendered":"<p>There are <i>n<\/i> bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it&#8217;s off or turning off if it&#8217;s on). For the <i>i<\/i>th round, you toggle every <i>i<\/i> bulb. For the <i>n<\/i>th round, you only toggle the last bulb. Find how many bulbs are on after <i>n<\/i> rounds.<br \/>\n<b>Example:<\/b><\/p>\n<pre class=\"\">Given <i>n<\/i> = 3.\nAt first, the three bulbs are <b>[off, off, off]<\/b>.\nAfter first round, the three bulbs are <b>[on, on, on]<\/b>.\nAfter second round, the three bulbs are <b>[on, off, on]<\/b>.\nAfter third round, the three bulbs are <b>[on, off, off]<\/b>.\nSo you should return 1, because there is only one bulb is on.<\/pre>\n<h3>\u6211\u7684\u8d85\u65f6\u89e3<\/h3>\n<p>\u9012\u63a8\u6cd5\uff0c\u5f53\u4ecen-1\u5230n\u65f6\uff0c\u65b0\u52a0\u4e0a\u53bb\u7684\u4e00\u6b21\u64cd\u4f5c\u662f\u5224\u65ad\u4ece[1,n]\u91cc\u9762n\u7684\u56e0\u5b50\u4e2a\u6570\uff0c\u8fd9\u91cc\u8bb0\u4f5cf(n)\u597d\u4e86\uff0c\u5373a[n] = a[n-1] + f(n)<br \/>\n\u56e0\u5b50\u4e2a\u6570\u7684\u6c42\u6cd5\u53ef\u4ee5\u7cbe\u7b80\u4e00\u4e0b\u65f6\u95f4\uff0c\u6392\u96640,1,2\u8fd9\u4e09\u4e2a\u7279\u6b8a\u503c\u540e\uff0c\u8ba1\u7b97[2, sqrt(n)]\u91cc\u5934\u80fd\u6574\u9664\u7684\u6570\u7684\u4e2a\u6570\u5c31\u884c\u4e86\uff0c\u5982\u679c\u78b0\u5230\u4e2a\u5e73\u65b9\u6570\u90a3\u4e2a\u6570+1\uff0c\u4e0d\u7136\u4e2a\u6570\u5c31\u662f+2\u7684\uff08i\u80fd\u88abn\u6574\u9664\uff0c\u90a3\u4e48\u5b83\u7684\u8865\u6570n\/i\u4e5f\u884c\uff09\u3002<br \/>\n\u8fd9\u4e2a\u65b9\u6cd5\u7684\u590d\u6742\u5ea6\u561b\uff0cO(n^1.5)\u5427<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int factors(int n){\n        if(n == 0){\n            return 0;\n        } else if(n == 1){\n            return 1;\n        } else if(n == 2){\n            return 2;\n        }\n        int count = 2;  \/\/1\u548c\u81ea\u5df1\n        int sqr = (int)(sqrt(n));\n        for(int i=2; i &lt;= sqr; i++){\n            if(n % i == 0){\n                if(i *i == n)\n                    count += 1; \/\/\u5e73\u65b9\u6570\n                else\n                    count += 2; \/\/\u4e00\u4e2a\u6570\u548c\u5b83\u7684\u8865\u6570\n            }\n        }\n        return count;\n    }\n    int bulbSwitch(int n) {\n        if(n &lt; 1){\n            return 0;\n        }\n        int bCount = 0;\n        for(int i=1; i&lt;=n; i++){\n            if((factors(i)&amp;1) == 1){\n                bCount ++;\n            }\n        }\n        return bCount;\n    }\n};<\/pre>\n<h3>\u667a\u5546\u78be\u538b\uff0cO(1)\u89e3<\/h3>\n<p>\u771f\u6b63\u7684\u5965\u4e49\u7528\u4e00\u884c\u4ee3\u7801\u5c31\u80fd\u89e3\u91ca<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int bulbSwitch(int n) {\n        return sqrt(n);\n    }\n};<\/pre>\n<p>\u5475\u5475\u54d2\uff01\u8fd9\u4e2a\u89c4\u5f8b\u5982\u679c\u591a\u8bd5\u51e0\u6b21\u8bf4\u4e0d\u5b9a\u80fd\u53d1\u73b0\uff0c0,1,1,1,2,2,2,2,2,3\u8fd9\u79cd\u6570\u5217\u561b<br \/>\n<a href=\"https:\/\/leetcode.com\/discuss\/91371\/share-my-o-1-solution-with-explanation\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/leetcode.com\/discuss\/91371\/share-my-o-1-solution-with-explanation<\/a><br \/>\n\u5927\u6982\u610f\u601d\u662f\uff0c\u8981\u5728[1,n]\u91cc\u5934\u627e\u54ea\u4e9b\u706f\u6ce1\u88ab\u6267\u884c\u4e86\u5947\u6570\u6b21\u64cd\u4f5c<\/p>\n<ul>\n<li>\u00a0\u5bf9\u4e8e\u4e00\u4e2a\u975e\u5e73\u65b9\u6570\u6765\u8bf4\uff08\u6bd4\u598212\uff09\uff0c\u56e0\u4e3a\u6709\u6210\u5bf9\u7684\u8865\u6570\uff081vs12; 2vs6\uff09\uff0c\u6240\u4ee5\u603b\u662f\u4f1a\u6309\u4e0b2\u7684\u500d\u6570\u6b21<\/li>\n<li>\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e2a\u5e73\u65b9\u6570\u6765\u8bf4\uff08\u6bd4\u598236\uff09\uff0c\u56e0\u4e3a\u5176\u4e2d\u6709\u4e2a\u6570(6)\u5b83\u7684\u8865\u6570\u662f\u81ea\u5df1\uff0c\u6240\u4ee5\u4f1a\u6309\u88ab\u4e0b\u5947\u6570\u6b21<\/li>\n<\/ul>\n<p>\u7136\u540e\u8fd9\u9053\u9898\u5c31\u53d8\u6210\u4e86\u627e[1,n]\u4e2d\u95f4\u6709\u51e0\u4e2a\u5e73\u65b9\u6570\u4e86<br \/>\nOMG&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it&#8217;s off or turning off if it&#8217;s on). For the ith round, you toggle every i bulb. For the nth round, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1974","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1974"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1974\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}