Skip to content

Practice Game of Life

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#289
LeetCode Problem

Game of Life

Problem Statement

The m by n array board contains live cells marked 1 and dead cells marked 0. A cell has up to eight neighbors: horizontally, vertically, and diagonally adjacent cells. In the next generation, a live cell survives only if it has two or three live neighbors; otherwise it dies. A dead cell becomes live only if it has exactly three live neighbors. Apply every change simultaneously using the original generation. Update board to that next generation in place; no return value is needed.

Source: Game of Life on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output
[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

# Example 2

Input
board = [[1,1],[1,0]]
Output
[[1,1],[1,1]]

Constraints

m == board.length
n == board[i].length
1 <= m, n <= 25
board[i][j] is 0 or 1.

Related LeetCode Problems

Practice more problems similar to Game of Life.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.