1375. Bulb Switcher III (M)

Bulb Switcher III (M)

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]

题意

按流程点亮指定位置的灯泡,点亮时灯泡为黄色,当且仅当它前面所有灯泡都被点亮时颜色变为蓝色。求整个点灯过程中有多少时刻点亮的灯泡全是蓝灯。

思路

三种方法:

  1. 从最初开始,设target为1,只有将灯target点亮后才有可能出现蓝灯,此时只要将灯target后面所有连着的黄灯变为蓝灯,并判断剩余黄灯数是否为0,是则说明此时刻全部为蓝灯,结果数+1;target位置移动后,重复操作直到点完所有灯。
  2. 使用Set处理。用pos指向点亮的编号最大的灯,如果新点亮的灯index在pos之后,则将pos到index之间所有灯的编号存入Set,并更新 pos = index;如果新点亮的灯index在pos之前,则在Set中找到index并将其移除。每一次操作后都判断当前Set是否为空,若空则说明所有灯都是蓝灯。这里Set保存的是:要使当前所有黄灯变为蓝灯所需要点亮的灯。
  3. LeetCode社区解答 - lee215:设指针right指向点亮的最大编号的灯,每次点亮新灯更新right并判断right是不是等于已经点亮的灯的个数,如果是说明当前所有点亮的灯都是蓝灯。 O ( 1 ) O(1) O(1)空间解法,🐂🍺就完事了。

代码实现 - 遍历

class Solution {
    public int numTimesAllBlue(int[] light) {
        boolean[] on = new boolean[light.length + 1];
        int target = 1;
        int yellow = 0, count = 0;
        for (int i = 0; i < light.length; i++) {
            int index = light[i];
            on[index] = true;
            yellow++;
            if (index == target) {
                while (target <= light.length && on[target]) {
                    target++;
                    yellow--;
                }
                if (yellow == 0) {
                    count++;
                }
            }
        }
        return count;
    }
}

代码实现 - Set

class Solution {
    public int numTimesAllBlue(int[] light) {
        Set<Integer> toOn = new HashSet<>();
        int pos = 0;
        int count = 0;
        for (int i = 0; i < light.length; i++) {
            int index = light[i];
            if (index > pos) {
                for (int j = pos + 1; j < index; j++) {
                    toOn.add(j);
                }
                pos = index;
            } else {
                toOn.remove(index);
            }
            if (toOn.isEmpty()) {
                count++;
            }
        }
        return count;
    }
}

代码实现 - O(1)空间

class Solution {
    public int numTimesAllBlue(int[] light) {
        int right = 0;
        int count = 0;
        for (int i = 0; i < light.length; i++) {
            right = Math.max(right, light[i]);
            if (right == i + 1) {
                count++;
            }
        }
        return count;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值