Skip to content

Commit f8f4288

Browse files
add 1375
1 parent a267360 commit f8f4288

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1375|[ Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1375.java) | |Medium||Array
1112
|1374|[Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1374.java) | |Easy||String
1213
|1373|[Maximum Sum BST in Binary Tree](https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1373.java) | |Hard||DP, BST
1314
|1372|[Longest ZigZag Path in a Binary Tree](https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1372.java) | |Hard||DP, Tree
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1375;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1375Test {
10+
private static _1375.Solution1 solution1;
11+
private static int[] light;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1375.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
light = new int[]{2, 1, 3, 5, 4};
21+
assertEquals(3, solution1.numTimesAllBlue(light));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
light = new int[]{3, 2, 4, 1, 5};
27+
assertEquals(2, solution1.numTimesAllBlue(light));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
light = new int[]{1, 2, 3, 4, 5, 6};
33+
assertEquals(6, solution1.numTimesAllBlue(light));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)