Game of Life
Game of Life is LeetCode 289 (Medium). It asks for one step of Conway's Game of Life. The board is an m × n grid where 1 is a live cell and 0 is a dead one. Each cell has up to eight neighbours: left, right, up, down and the four diagonals.
- A live cell with 2 or 3 live neighbours stays live; with fewer or more, it dies.
- A dead cell with exactly 3 live neighbours becomes live.
All cells change at the same moment, so every rule reads the board as it was before the step. Update board in place; the function returns nothing.
- m == board.length
- n == board[i].length
- 1 <= m, n <= 25
- board[i][j] is 0 or 1.
Intuition
The trap is order. If you overwrite a cell and then count it as a neighbour of the next cell, that count sees the new board, not the old one.
The fix is to let each cell hold both states at once. Bit 0 keeps the current state and bit 1 records the next one. Counting reads only bit 0 (cell & 1), and no write in the first pass touches bit 0, so every count comes from the old board. When every cell has been decided, shifting each value right by one moves the next state into bit 0. The board itself is the scratch space, so no copy is needed.
"Update every cell at once, in place" means the old and new values must live side by side. When a cell holds 0 or 1, the spare bits of the integer are free storage.
Approach
Before reading on, write the rules as one test: when is a cell live in the next state? Then decide what number to store in a cell that is live now but dies, and in one that is dead now but is born, so that a neighbour can still read the old state.
Two ways to keep the old board readable
Bit 0 holds the current state, bit 1 the next. Count with & 1, then shift every cell right by one.
- In place: meets the follow-up with no extra board.
- One test: 3 neighbours, or 2 and live now.
Copy the grid, count neighbours in the copy, and write plain 0 or 1 into board.
- Simplest to read: no bit tricks.
- Costs a full copy: fails the in-place follow-up.
Both read neighbours from the old state and run in O(m · n) time. The steps, code and live demo use the two-bit trick; the copy version follows the demo.
Count live neighbours from bit 0
For cell (r, c), visit every cell in the 3 × 3 block around it except itself, and add board[nr][nc] & 1. Clamp the block with max and min so cells on the border only look inside the grid.
Decide the next state with one test
The cell is live next time if it has exactly 3 live neighbours, or exactly 2 and is live now. Those are the only two ways to end live, so every other case ends dead and needs no write.
Store the next state in bit 1
When the cell will be live, do board[r][c] |= 2. That sets bit 1 and leaves bit 0 alone, so cells checked later still read this cell's old state.
Shift every cell to finish
In a second pass, do board[r][c] >>= 1. A 3 or a 2 becomes 1 (live next), and a 1 or a 0 becomes 0 (dead next). It has to be a separate pass: shifting during the first pass would erase bit 0 before every neighbour had read it.
Code in Python, C++ and Java
Copy the board first
old is a snapshot of the board before any change. Every count reads old, so writing the new value into board straight away is safe. It is the easiest version to get right, at the cost of a second grid.
Common pitfalls
Writing the new state straight into the cell
board[r][c] = 1 if live == 3 or (live == 2 and board[r][c]) else 0
if live == 3 or (live == 2 and board[r][c] & 1):
board[r][c] |= 2Cells checked later count the new value instead of the old one. On the vertical line [[0,1,0],[0,1,0],[0,1,0]] the top cell dies first, so the cell below-left of it counts 2 neighbours instead of 3 and is never born. The result is an empty board; the right answer is the horizontal line [[0,0,0],[1,1,1],[0,0,0]].
Counting a neighbour without masking bit 0
live += board[nr][nc]
live += board[nr][nc] & 1
After marking, a cell that stays live holds 3 and a newborn holds 2, so unmasked they add too much. On [[1,1],[1,0]] the top-left cell is marked first and holds 3; the next two cells then count 4 and die, and the bottom-right cell counts 5 and is never born. The output is [[1,0],[0,0]] instead of [[1,1],[1,1]].
Counting the cell itself
for nr in range(max(r - 1, 0), min(r + 2, rows)):
for nc in range(max(c - 1, 0), min(c + 2, cols)):
live += board[nr][nc] & 1 if (nr, nc) != (r, c):
live += board[nr][nc] & 1The 3 × 3 block includes the centre cell. In the square [[1,1],[1,1]] every cell has 3 live neighbours and should stay live, but with itself counted it sees 4 and dies, so the whole board comes out dead.
Edge cases
[[1]]The clamped block holds only the cell itself, which is skipped, so the count is 0 and the cell dies: [[0]].
Complexity
Game of Life FAQ
How would you handle an infinite board?
Stop storing a grid. Keep only the live cells as a set of (row, col) pairs. For each live cell, add 1 to a counter for each of its 8 neighbours. The next live set is every cell whose counter is 3, plus every live cell whose counter is 2. The work grows with the number of live cells, not with the size of the board, and there is no border to fall off.
Other solutions mark cells with −1 and 2. Is that different?
It is the same idea with different labels: one marker means "live now, dead next" and the other "dead now, live next". Counting must then treat one marker as live and the other as dead, and a final pass maps each marker to 0 or 1. The bit version needs no special cases: & 1 is the old state and >> 1 is the new one.