Created
December 13, 2019 23:54
-
-
Save akshay-ravindran-96/44b6b814f6df76792dd75c5d23654931 to your computer and use it in GitHub Desktop.
prison_cells_after_N_days.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] prisonAfterNDays(int[] cells, int N) { | |
int cycle =1; | |
int first[] = new int[8]; | |
for(int i=1; i<7; i++) | |
{ | |
first[i] = (cells[i-1] == cells[i+1] ? 1 :0); | |
} | |
N-=1; | |
for(int i=0; i<8; i++) | |
cells[i] = first[i]; | |
while(N-- >0) | |
{ | |
int temp [] = new int [8]; | |
for(int i=1; i<7; i++) | |
{ | |
temp[i] = (cells[i-1] == cells[i+1] ? 1 :0); | |
} | |
if(Arrays.equals(temp, first)) N%=cycle; | |
for(int i=0; i<8; i++) | |
cells[i] = temp[i]; | |
cycle++; | |
} | |
return cells; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment