Skip to content

Instantly share code, notes, and snippets.

@akshay-ravindran-96
Created December 13, 2019 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshay-ravindran-96/44b6b814f6df76792dd75c5d23654931 to your computer and use it in GitHub Desktop.
Save akshay-ravindran-96/44b6b814f6df76792dd75c5d23654931 to your computer and use it in GitHub Desktop.
prison_cells_after_N_days.java
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