Skip to content
Decoder

Game of Life

Medium

Problem

A grid board of m rows and n columns holds cells that are alive (1) or dead (0). Each cell has up to eight neighbours: the cells touching it horizontally, vertically and diagonally. The next generation follows two rules:

- A live cell with two or three live neighbours stays alive; any other live cell dies. - A dead cell with exactly three live neighbours comes alive; any other dead cell stays dead.

Every cell changes at the same moment, so each cell's fate depends only on the current generation. Update board to the next generation in place. The function returns nothing; the tests inspect board after the call.

Example 1

[[0, 1, 0],          [[0, 0, 0],

Example 2

Input
[0, 1, 0],
Output
[1, 1, 1],

Example 3

 [0, 1, 0]]           [0, 0, 0]]

Example 4

[[0, 1],             [[1, 1],

Example 5

Input
[1, 1]]
Output
[1, 1]]

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.