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 INPUT Initial Board State (m x n grid) 0 1 0 0 0 1 1 1 1 0 0 0 = Live (1) = Dead (0) board = [ [0,1,0], [0,0,1], [1,1,1], [0,0,0] ] ALGORITHM STEPS 1 State Encoding Use special values: 2 = was alive, now dead 3 = was dead, now alive 2 Count Neighbors For each cell, count 8 adjacent live cells 3 Apply Rules Live + neighbors < 2 --> Dies Live + 2-3 neighbors --> Lives Live + neighbors > 3 --> Dies Dead + 3 neighbors --> Lives 4 Decode States Convert encoded values back to 0 or 1 State Encoding Table 0: dead --> dead 1: live --> live 2: live --> dead 3: dead --> live FINAL RESULT Next Generation State 0 0 0 1 0 1 0 1 1 0 1 0 OK - In-Place Update Complete Output: [0,0,0], [1,0,1], [0,1,1], [0,1,0] Key Insight: By using state encoding (2 and 3), we can track both the original state and the new state simultaneously. This allows in-place modification with O(1) extra space. When counting neighbors, we check if value % 2 == 1 to determine the original state. After processing all cells, we decode: values 2,3 become value / 2 for final state. Time: O(m*n) | Space: O(1) - truly in-place without copying the board! TutorialsPoint - Game of Life | In-Place with State Encoding Approach
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