Closed congr closed 5 years ago
class Solution {
public int[] prisonAfterNDays(int[] cells, int N) {
// find day1 cells
int[] day1 = new int[8];
for (int i = 1; i < 7; i++)
day1[i] = cells[i-1] == cells[i+1] ? 1 : 0;
// find cycle
int cycle = 0;
while (N-- > 0) {
int[] copy = new int[8];
for (int i = 1; i < 7; i++)
copy[i] = cells[i-1] == cells[i+1] ? 1 : 0;
if (cycle > 1 && Arrays.equals(day1, copy)) N %= cycle; // !!!
cells = copy.clone(); // !!!
System.out.println(Arrays.toString(cells));
cycle++;
}
return cells;
}
}
https://leetcode.com/problems/prison-cells-after-n-days/