-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuly3_2020.java
36 lines (35 loc) · 1.03 KB
/
July3_2020.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Prison cells after N days => find the cycle
class Solution {
public int[] prisonAfterNDays(int[] cells, int N) {
Set<String> set = new HashSet<>();
int size = 0;
boolean flag = false;
for(int i = 0; i < N; i++){
int[] nextState = new int[8];
for(int j = 1; j < 7; j++){
nextState[j] = (cells[j - 1] == cells[j + 1] ? 1 : 0);
}
String s = Arrays.toString(nextState);
if(!set.contains(s)){
set.add(s);
size++;
}
else{
flag = true;
break;
}
cells = nextState;
}
if(flag){
N = N % size;
for(int i = 0; i < N; i++){
int[] nextState = new int[8];
for(int j = 1; j < 7; j++){
nextState[j] = (cells[j - 1] == cells[j + 1] ? 1 : 0);
}
cells = nextState;
}
}
return cells;
}
}