Game of Life - Problem

According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules:

  • Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  • Any live cell with two or three live neighbors lives on to the next generation.
  • Any live cell with more than three live neighbors dies, as if by over-population.
  • Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The next state of the board is determined by applying the above rules simultaneously to every cell in the current state of the m x n grid board. In this process, births and deaths occur simultaneously.

Given the current state of the board, update the board to reflect its next state.

Note: You do not need to return anything.

Input & Output

Example 1 — Standard Pattern
$ 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]]
💡 Note: The bottom row [1,1,1] creates a classic 'blinker' pattern. Cell at (1,0) gets exactly 3 neighbors so becomes alive. Cell at (1,1) dies from under-population.
Example 2 — All Dead
$ Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]
💡 Note: Top-left has 2 neighbors (survives), top-right has 2 neighbors (survives), bottom-left has 2 neighbors (survives), bottom-right has 3 neighbors (becomes alive).

Constraints

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

Visualization

Tap to expand
Conway's Game of Life: Cellular EvolutionGeneration 00100001011100000Rules Applied• Live cell < 2 neighbors → Dies• Live cell 2-3 neighbors → Lives• Live cell > 3 neighbors → Dies• Dead cell = 3 neighbors → LivesGeneration 10000101001100100Simultaneously count neighbors and apply rules to all cellsThe classic "blinker" pattern emerges!
Understanding the Visualization
1
Input Board
Grid of live (1) and dead (0) cells
2
Apply Rules
Count neighbors and apply Conway's rules simultaneously
3
Output Board
Updated grid showing next generation
Key Takeaway
🎯 Key Insight: All cell updates must happen simultaneously based on the current state
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 10
95.0K Views
Medium Frequency
~15 min Avg. Time
4.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen