File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /*
3
+ prison state repeating after a certian number of iterations.
4
+ */
5
+ public int [] prisonAfterNDays (int [] cells , int N ) {
6
+ HashSet <String > set = new HashSet <>();
7
+ int times = 0 ;
8
+ boolean flag = false ;
9
+ for (int i =0 ; i <N ; i ++) {
10
+ int [] nextArray = nextDays (cells );
11
+ String arrString = Arrays .toString (nextArray );
12
+ if (!set .contains (arrString )) {
13
+ set .add (arrString );
14
+ times ++;
15
+ } else {
16
+ flag = true ;
17
+ break ;
18
+ }
19
+ cells = nextArray ;
20
+ }
21
+ if (flag ) {
22
+ N = N %times ;
23
+ for (int i =0 ; i <N ; i ++) {
24
+ cells = nextDays (cells );
25
+ }
26
+ }
27
+ return cells ;
28
+ }
29
+
30
+ public int [] nextDays (int [] cells ) {
31
+ int [] res = new int [cells .length ];
32
+ for (int i =1 ; i <cells .length -1 ; i ++){
33
+ if (cells [i -1 ] == cells [i +1 ]) {
34
+ res [i ] = 1 ;
35
+ }
36
+ }
37
+ return res ;
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments