Skip to content
Onsite

Game of Life

MediumP3Coding

The board is an m x n grid of cells, each alive (1) or dead (0). Each cell has eight neighbours (horizontal, vertical and diagonal). Cells outside the board count as dead.

Compute the next generation, where all cells change at the same time using these rules:

  • A live cell with fewer than 2 live neighbours dies.
  • A live cell with 2 or 3 live neighbours stays alive.
  • A live cell with more than 3 live neighbours dies.
  • A dead cell with exactly 3 live neighbours becomes alive.

Update board in place; the function returns nothing.

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]]

Example 3

Input
board = [[0]]
Output
[[0]]

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 12.