{"id":6415,"date":"2020-03-08T11:54:20","date_gmt":"2020-03-08T18:54:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6415"},"modified":"2020-03-08T11:54:54","modified_gmt":"2020-03-08T18:54:54","slug":"leetcode-1375-bulb-switcher-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1375-bulb-switcher-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1375. Bulb Switcher III"},"content":{"rendered":"\n<p>There is a room with&nbsp;<code>n<\/code>&nbsp;bulbs, numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>, arranged in a row from left to right. Initially, all the bulbs are turned off.<\/p>\n\n\n\n<p>At moment&nbsp;<em>k<\/em>&nbsp;(for&nbsp;<em>k<\/em>&nbsp;from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>), we turn on the&nbsp;<code>light[k]<\/code>&nbsp;bulb. A bulb&nbsp;<strong>change&nbsp;color to blue<\/strong>&nbsp;only if it is on and all the previous bulbs (to the left)&nbsp;are turned on too.<\/p>\n\n\n\n<p>Return the number of moments in&nbsp;which&nbsp;<strong>all&nbsp;turned on<\/strong>&nbsp;bulbs&nbsp;<strong>are blue.<\/strong><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/29\/sample_2_1725.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> light = [2,1,3,5,4]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> All bulbs turned on, are blue at the moment 1, 2 and 4.\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> light = [3,2,4,1,5]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> All bulbs turned on, are blue at the moment 3, and 4 (index-0).\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> light = [4,1,2,3]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> All bulbs turned on, are blue at the moment 3 (index-0).\nBulb 4th changes to blue at the moment 3.\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> light = [2,1,4,3,6,5]\n<strong>Output:<\/strong> 3\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> light = [1,2,3,4,5,6]\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n ==&nbsp;light.length<\/code><\/li><li><code>1 &lt;= n &lt;= 5 * 10^4<\/code><\/li><li><code>light<\/code>&nbsp;is a permutation of&nbsp;&nbsp;<code>[1, 2, ..., n]<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution<\/strong><\/h2>\n\n\n\n<p>Track the right most light l_k, all turned-on lights are blue if and only if the right most one is k, and there are exact k lights on right now.<\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numTimesAllBlue(vector<int>& light) {    \n    int ans = 0;\n    int right = 0;\n    for (int i = 0; i < light.size(); ++i) {\n      right = max(right, light[i]);\n      ans += right == i + 1;\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;1&nbsp;to&nbsp;n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment&nbsp;k&nbsp;(for&nbsp;k&nbsp;from&nbsp;0&nbsp;to&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":[184],"tags":[20,376],"class_list":["post-6415","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6415","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=6415"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6415\/revisions"}],"predecessor-version":[{"id":6418,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6415\/revisions\/6418"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}