Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.
You are given an integer array cells where cells[i] == 1 if the ith cell is occupied and cells[i] == 0 if the ith cell is vacant, and you are given an integer n.
Return the state of the prison after n days (i.e., n such changes described above).
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], n = 7 Output: [0,0,1,1,0,0,0,0] Explanation: The following table summarizes the state of the prison on each day: Day 0: [0, 1, 0, 1, 1, 0, 0, 1] Day 1: [0, 1, 1, 0, 0, 0, 0, 0] Day 2: [0, 0, 0, 0, 1, 1, 1, 0] Day 3: [0, 1, 1, 0, 0, 1, 0, 0] Day 4: [0, 0, 0, 0, 0, 1, 0, 0] Day 5: [0, 1, 1, 1, 0, 1, 0, 0] Day 6: [0, 0, 1, 0, 1, 1, 0, 0] Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], n = 1000000000 Output: [0,0,1,1,1,1,1,0]
Constraints:
cells.length == 8cells[i] is either 0 or 1.1 <= n <= 109Problem summary: 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. Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors. You are given an integer array cells where cells[i] == 1 if the ith cell is occupied and cells[i] == 0 if the ith cell is vacant, and you are given an integer n. Return the state of the prison after n days (i.e., n such changes described above).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math · Bit Manipulation
[0,1,0,1,1,0,0,1] 7
[1,0,0,1,0,0,1,0] 1000000000
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #957: Prison Cells After N Days
class Solution {
public int[] prisonAfterNDays(int[] cells, int n) {
int[] firstDayCells = new int[cells.length];
int[] nextDayCells = new int[cells.length];
for (int day = 0; n-- > 0; cells = nextDayCells.clone(), ++day) {
for (int i = 1; i + 1 < cells.length; ++i)
nextDayCells[i] = cells[i - 1] == cells[i + 1] ? 1 : 0;
if (day == 0)
firstDayCells = nextDayCells.clone();
else if (Arrays.equals(nextDayCells, firstDayCells))
n %= day;
}
return cells;
}
}
// Accepted solution for LeetCode #957: Prison Cells After N Days
// Auto-generated Go example from java.
func exampleSolution() {
}
// Reference (java):
// // Accepted solution for LeetCode #957: Prison Cells After N Days
// class Solution {
// public int[] prisonAfterNDays(int[] cells, int n) {
// int[] firstDayCells = new int[cells.length];
// int[] nextDayCells = new int[cells.length];
//
// for (int day = 0; n-- > 0; cells = nextDayCells.clone(), ++day) {
// for (int i = 1; i + 1 < cells.length; ++i)
// nextDayCells[i] = cells[i - 1] == cells[i + 1] ? 1 : 0;
// if (day == 0)
// firstDayCells = nextDayCells.clone();
// else if (Arrays.equals(nextDayCells, firstDayCells))
// n %= day;
// }
//
// return cells;
// }
// }
# Accepted solution for LeetCode #957: Prison Cells After N Days
class Solution:
def prisonAfterNDays(self, cells: list[int], n: int) -> list[int]:
nextDayCells = [0] * len(cells)
day = 0
while n > 0:
n -= 1
for i in range(1, len(cells) - 1):
nextDayCells[i] = 1 if cells[i - 1] == cells[i + 1] else 0
if day == 0:
firstDayCells = nextDayCells.copy()
elif nextDayCells == firstDayCells:
n %= day
cells = nextDayCells.copy()
day += 1
return cells
// Accepted solution for LeetCode #957: Prison Cells After N Days
struct Solution;
use std::collections::HashMap;
impl Solution {
fn prison_after_n_days(mut cells: Vec<i32>, mut n: i32) -> Vec<i32> {
let mut hm: HashMap<Vec<i32>, i32> = HashMap::new();
while n > 0 {
hm.insert(cells.to_vec(), n);
let mut next = vec![0; 8];
for i in 1..7 {
next[i] = 1 - (cells[i - 1] ^ cells[i + 1]);
}
cells = next;
n -= 1;
if let Some(m) = hm.get(&cells) {
n %= m - n;
}
}
cells
}
}
#[test]
fn test() {
let cells = vec![0, 1, 0, 1, 1, 0, 0, 1];
let n = 7;
let res = vec![0, 0, 1, 1, 0, 0, 0, 0];
assert_eq!(Solution::prison_after_n_days(cells, n), res);
let cells = vec![1, 0, 0, 1, 0, 0, 1, 0];
let n = 1_000_000_000;
let res = vec![0, 0, 1, 1, 1, 1, 1, 0];
assert_eq!(Solution::prison_after_n_days(cells, n), res);
}
// Accepted solution for LeetCode #957: Prison Cells After N Days
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #957: Prison Cells After N Days
// class Solution {
// public int[] prisonAfterNDays(int[] cells, int n) {
// int[] firstDayCells = new int[cells.length];
// int[] nextDayCells = new int[cells.length];
//
// for (int day = 0; n-- > 0; cells = nextDayCells.clone(), ++day) {
// for (int i = 1; i + 1 < cells.length; ++i)
// nextDayCells[i] = cells[i - 1] == cells[i + 1] ? 1 : 0;
// if (day == 0)
// firstDayCells = nextDayCells.clone();
// else if (Arrays.equals(nextDayCells, firstDayCells))
// n %= day;
// }
//
// return cells;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.