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|灯泡开关
61 0
|
算法 C++ Python
【每日算法Day 61】LeetCode 672. 灯泡开关 Ⅱ
【每日算法Day 61】LeetCode 672. 灯泡开关 Ⅱ
|
算法 C++
每日算法系列【LeetCode 319】灯泡开关
每日算法系列【LeetCode 319】灯泡开关
103 0
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
|
机器学习/深度学习
LeetCode每日一题——672. 灯泡开关 Ⅱ
房间中有 n 只已经打开的灯泡,编号从 1 到 n 。墙上挂着 4 个开关 。
76 0
|
机器学习/深度学习 自然语言处理 算法
|
15天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
15天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-1
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
16天前
|
索引
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
|
16天前
|
算法
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)