Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
957. Prison Cells After N Days-There are 8 prison cells in a row, and each cell is either occupied or
vacant. Each day, whether the cell is occupied or vacant changes according to the following rules :
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
Given the initial state of the prison, return the state of the prison after N days(and N such changes
described above.)
Sol- Because there are at most 256(8 digit no) possible states for the prison, eventually the states repeat into a cycle rather
quickly. We can keep track of when the states repeat to find the period t of this cycle, and skip days in multiples of t.
Algorithm
return result;
}