|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 1375. Bulb Switcher III |
| 5 | + * |
| 6 | + * 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. |
| 7 | + * 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. |
| 8 | + * Return the number of moments in which all turned on bulbs are blue. |
| 9 | + * |
| 10 | + * Example 1: |
| 11 | + * Input: light = [2,1,3,5,4] |
| 12 | + * Output: 3 |
| 13 | + * Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. |
| 14 | + * |
| 15 | + * Example 2: |
| 16 | + * Input: light = [3,2,4,1,5] |
| 17 | + * Output: 2 |
| 18 | + * Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). |
| 19 | + * |
| 20 | + * Example 3: |
| 21 | + * Input: light = [4,1,2,3] |
| 22 | + * Output: 1 |
| 23 | + * Explanation: All bulbs turned on, are blue at the moment 3 (index-0). |
| 24 | + * Bulb 4th changes to blue at the moment 3. |
| 25 | + * |
| 26 | + * Example 4: |
| 27 | + * Input: light = [2,1,4,3,6,5] |
| 28 | + * Output: 3 |
| 29 | + * |
| 30 | + * Example 5: |
| 31 | + * Input: light = [1,2,3,4,5,6] |
| 32 | + * Output: 6 |
| 33 | + * |
| 34 | + * Constraints: |
| 35 | + * n == light.length |
| 36 | + * 1 <= n <= 5 * 10^4 |
| 37 | + * light is a permutation of [1, 2, ..., n] |
| 38 | + * */ |
| 39 | +public class _1375 { |
| 40 | + public static class Solution1 { |
| 41 | + public int numTimesAllBlue(int[] light) { |
| 42 | + int blues = 0; |
| 43 | + int[] status = new int[light.length];//0 means off, 1 means on |
| 44 | + for (int i = 0; i < light.length; i++) { |
| 45 | + status[light[i] - 1] = 1; |
| 46 | + if (checkAllBlues(status, i)) { |
| 47 | + blues++; |
| 48 | + } |
| 49 | + } |
| 50 | + return blues; |
| 51 | + } |
| 52 | + |
| 53 | + private boolean checkAllBlues(int[] status, int endIndex) { |
| 54 | + for (int i = 0; i <= endIndex; i++) { |
| 55 | + if (status[i] != 1) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments