Game of Life

Encode next-state in the high bit so each cell carries (current, next) without extra storage.

Medium
Time O(m*n) Space O(1)
LeetCode
6 min read
matrix bit-manipulation in-place

Problem#

Apply one step of Conway’s Game of Life to the input grid in place. Rules: a live cell with 2 or 3 live neighbors stays live; a dead cell with exactly 3 live neighbors becomes live; otherwise dies/stays dead.

Examples#

  • [[0,1,0],[0,0,1],[1,1,1],[0,0,0]][[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
  • [[1,1],[1,0]][[1,1],[1,1]]

Constraints#

  • m == board.length, n == board[i].length, 1 <= m, n <= 25.

Approach#

Encode the next state in bit 1 while reading the current state from bit 0. After the sweep, shift everything right by one bit. Each cell counts live neighbors using board[ni][nj] & 1.

Solution#

Game of Life
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size(), n = board[0].size();
int dx[8] = {1,1,1,0,0,-1,-1,-1}, dy[8] = {1,0,-1,1,-1,1,0,-1};
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int live = 0;
for (int k = 0; k < 8; ++k) {
int ni = i + dx[k], nj = j + dy[k];
if (ni < 0 || ni >= m || nj < 0 || nj >= n) continue;
live += board[ni][nj] & 1;
}
int cur = board[i][j] & 1;
int next = (cur && (live == 2 || live == 3)) || (!cur && live == 3);
board[i][j] |= (next << 1);
}
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
board[i][j] >>= 1;
}
};

Editorial#

The two-bit encoding next_state << 1 | current_state keeps both states simultaneously. Subsequent cells still read the original state via & 1, so the update order doesn’t corrupt counting. A final shift extracts the next generation.

Complexity#

  • Time: O(m*n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.