Game of Life
The board is made up of an m x n grid of cells, where each cell is either alive (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the rules of Conway's Game of Life. Given the current state of the board, return the next state.
Sample Input
board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Sample Output
[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Constraints
- 1 <= m, n <= 25
- board[i][j] is 0 or 1
Test Cases
Case 1
Args: [[[0,1,0],[0,0,1],[1,1,1],[0,0,0]]]
Expected: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Topics
To update in-place, encode transitions with extra states: 2 means alive → dead, 3 means dead → alive. Count live neighbors using original states (values 1 or 2), then do a final pass to map states back to 0/1.
function gameOfLife(board) {
const m = board.length, n = board[0].length;
const dirs = [-1, 0, 1];
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let live = 0;
for (const di of dirs) {
for (const dj of dirs) {
if (di === 0 && dj === 0) continue;
const ni = i + di, nj = j + dj;
if (ni >= 0 && ni < m && nj >= 0 && nj < n) {
if (board[ni][nj] === 1 || board[ni][nj] === 2) live++;
}
}
}
if (board[i][j] === 1 && (live < 2 || live > 3)) board[i][j] = 2;
if (board[i][j] === 0 && live === 3) board[i][j] = 3;
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === 2) board[i][j] = 0;
if (board[i][j] === 3) board[i][j] = 1;
}
}
}
Time: O(m × n) Space: O(1)
Saved in this browser only. Private to you.