Back to problems Sign in to unlock
Leetcode
Medium
Matrices
Arrays
Simulation
Google
Game Of Life
Update a 2D board one generation at a time using the rules of Conway's Game of Life.
Acceptance 100%
Problem Statement
You are given an board where each cell is either alive (1) or dead (0). The board evolves in discrete steps according to the following rules:
- Any live cell with fewer than 2 live neighbors dies, as if by underpopulation.
- Any live cell with 2 or 3 live neighbors lives on.
- Any live cell with more than 3 live neighbors dies, as if by overpopulation.
- Any dead cell with exactly 3 live neighbors becomes alive, as if by reproduction.
A cell's neighbors are the 8 surrounding cells horizontally, vertically, and diagonally adjacent.
Modify the board in place to represent the next generation.
Input Format
- A 2D integer array
boardof sizem x n. board[i][j] = 1means the cell is alive, and0means it is dead.
Output Format
- Update
boardin place so that it reflects the next state of the grid. - No separate return value is required in the in-place formulation.
Constraints
1 <= m, n- The grid is finite and cells outside the board are treated as dead.
- Use only the current generation to determine the next one.
Examples
Sample cases returned by the problem API.
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]]
Explanation
Apply the four rules to every cell simultaneously using the original board state. The resulting next generation is shown in the output.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.