Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7f87bfb

Browse files
add 957
1 parent 34969d8 commit 7f87bfb

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ _If you like this project, please leave me a star._ ★
234234
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | |Easy| DFS, recursion|
235235
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | |Easy|
236236
|958|[Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_958.java) | |Medium|Tree
237+
|957|[Prison Cells After N Days](https://leetcode.com/problems/prison-cells-after-n-days/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_957.java) | |Medium|
237238
|954|[Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_954.java) | [:tv:](https://youtu.be/Q0WKzdpR74o)|Medium|
238239
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java)| |Easy|
239240
|951|[Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_951.java) | |Medium| Tree, DFS, recursion|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class _957 {
8+
public static class Solution1 {
9+
public int[] prisonAfterNDays(int[] cells, int N) {
10+
Set<String> prisonStates = new HashSet<>();
11+
boolean hasCycle = false;
12+
int times = 0;
13+
for (int i = 0; i < N; i++) {
14+
int[] next = getNextDay(cells);
15+
String nextDayState = Arrays.toString(next);
16+
if (prisonStates.contains(nextDayState)) {
17+
hasCycle = true;
18+
break;
19+
} else {
20+
prisonStates.add(nextDayState);
21+
times++;
22+
}
23+
cells = next;
24+
}
25+
if (hasCycle) {
26+
N %= times;
27+
for (int i = 0; i < N; i++) {
28+
cells = getNextDay(cells);
29+
}
30+
}
31+
return cells;
32+
}
33+
34+
private int[] getNextDay(int[] cells) {
35+
int[] nextDay = new int[cells.length];
36+
for (int i = 0; i < cells.length; i++) {
37+
if (i == 0 || i == cells.length - 1) {
38+
nextDay[i] = 0;
39+
} else {
40+
nextDay[i] = (cells[i - 1] == cells[i + 1]) ? 1 : 0;
41+
}
42+
}
43+
return nextDay;
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._957;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _957Test {
10+
private static _957.Solution1 solution1;
11+
private static int[] cells;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _957.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
cells = new int[]{0, 1, 0, 1, 1, 0, 0, 1};
21+
assertArrayEquals(new int[]{0, 0, 1, 1, 0, 0, 0, 0}, solution1.prisonAfterNDays(cells, 7));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
cells = new int[]{1, 0, 0, 1, 0, 0, 1, 0};
27+
assertArrayEquals(new int[]{0, 0, 1, 1, 1, 1, 1, 0}, solution1.prisonAfterNDays(cells, 1000000000));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
cells = new int[]{0, 1, 1, 1, 0, 0, 0, 0};
33+
assertArrayEquals(new int[]{0, 0, 1, 0, 0, 1, 1, 0}, solution1.prisonAfterNDays(cells, 99));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)