LeetCode 1375. 灯泡开关 III

简介: LeetCode 1375. 灯泡开关 III

LeetCode 1375. 灯泡开关 III


Table of Contents

一、中文版

二、英文版

三、My answer

四、解题报告

一、中文版

房间中有 n 枚灯泡,编号从 1 到 n,自左向右排成一排。最初,所有的灯都是关着的。

在 k  时刻( k 的取值范围是 0 到 n - 1),我们打开 light[k] 这个灯。

灯的颜色要想 变成蓝色 就必须同时满足下面两个条件:

灯处于打开状态。

排在它之前(左侧)的所有灯也都处于打开状态。

请返回能够让 所有开着的 灯都 变成蓝色 的时刻 数目 。

示例 1:

输入:light = [2,1,3,5,4]

输出:3

解释:所有开着的灯都变蓝的时刻分别是 1,2 和 4 。

示例 2:

输入:light = [3,2,4,1,5]

输出:2

解释:所有开着的灯都变蓝的时刻分别是 3 和 4(index-0)。

示例 3:

输入:light = [4,1,2,3]

输出:1

解释:所有开着的灯都变蓝的时刻是 3(index-0)。

第 4 个灯在时刻 3 变蓝。

示例 4:

输入:light = [2,1,4,3,6,5]

输出:3

示例 5:

输入:light = [1,2,3,4,5,6]

输出:6

 

提示:

n == light.length

1 <= n <= 5 * 10^4

light 是 [1, 2, ..., n] 的一个排列。

通过次数3,461提交次数7,404

 

二、英文版

There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.


At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.


Return the number of moments in which all turned on bulbs are blue.

Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
 
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of  [1, 2, ..., n]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/bulb-switcher-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

三、My answer

class Solution:
    def numTimesAllBlue(self, light: List[int]) -> int:
        # version 1: 双重循环,超时    
        res = 0
        light_list = [0] * (1+len(light)) 
        for moment in range(len(light)):
            light_list[light[moment]] = 1
            sum_ = 0
            for j in range(1, moment+2): # 注意 j 从1开始
                sum_ += light_list[j]
            if sum_ == moment+1:
                res += 1
        return res
        # version 2:下标就是 moment, moment 是从 0 开始,灯是从 1 开始,所以 moment=最大灯-1
        max_light = 0
        res = 0
        for i in range(len(light)):
            max_light = max(max_light, light[i])
            if i == max_light - 1:
                res += 1
        return res

四、解题报告

version 1 是双重 for 循环,超时。

算法思想:列表 light_list 表示 时刻灯泡状态 list,moment 从 0 到 n-1,灯泡从 1 到 n。 light_list 的作用相当于标志位,如果 i 这个灯亮了,那么 light_list[light[i]] = 1。遍历每个灯,对于 时刻 moment,对应在列表 light_list 中的位置是 moment+1(light_list[0] = 0 一直没用上,因为没有 0 号灯),也就是前 moment + 1的灯都需要亮着。moment 的范围是 0 到 len(light)-1,也就是 0 到 n-1,符合一开始的限制条件。j 的范围是 2 到 moment+1,因为 j 表示亮灯的个数(moment 从 0 到 n-1,灯泡从 1 到 n,所以看出 灯的编号 = moment + 1)。

求和表示:在时刻 moment,前 moment+1 个灯应该量,才能保证亮着的灯都是蓝的。而不是看所有灯中亮的总数。

比如时刻2,首先应该有三个灯亮,其次应该是 1 2 3 号灯全亮,如果此时是 1 2 5 号灯亮,虽然也是亮着三个灯,但 5 号灯不是蓝色,不满足题意。  

version 2 是 version 1 的清晰简化版,一次 for 循环解决战斗。

根据上面解释(比如时刻2,首先应该有三个灯亮,其次应该是 1 2 3 号灯全亮,如果此时是 1 2 5 号灯亮,虽然也是亮着三个灯,但 5 号灯不是蓝色,不满足题意)只需要判断最大号亮着的灯与 moment 的关系即可。

相关文章
|
Python
Leetcode|灯泡开关
Leetcode|灯泡开关
180 0
|
算法 C++ Python
【每日算法Day 61】LeetCode 672. 灯泡开关 Ⅱ
【每日算法Day 61】LeetCode 672. 灯泡开关 Ⅱ
138 0
|
算法 C++
每日算法系列【LeetCode 319】灯泡开关
每日算法系列【LeetCode 319】灯泡开关
217 0
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
|
机器学习/深度学习 自然语言处理 算法
|
机器学习/深度学习
LeetCode每日一题——672. 灯泡开关 Ⅱ
房间中有 n 只已经打开的灯泡,编号从 1 到 n 。墙上挂着 4 个开关 。
141 0
|
12月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
170 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
131 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
283 2
|
12月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
175 3
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口