According to the 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.”
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
- 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.
Write a function to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
Example:
Input:
[
[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]
]
Follow up:
- Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
- In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
[code lang="java"]
class Solution {
public void gameOfLife(int[][] board) {
}
}
[code]
Idea – 1
We can use another

board to update. Time and space complexity

.
[code lang="java"]
class Solution {
public void gameOfLife(int[][] board) {
if(board==null || board.length==0)
{
return;
}
int m = board.length;
int n = board[0].length;
int[][] tmp = new int[m][n];
for(int row = 0; row < m; ++row)
{
for(int col = 0; col < n; ++col)
{
int aliveNeighbors = countLiveNeighbors(board, row, col);
if(board[row][col]==0)
{
tmp[row][col] = (aliveNeighbors==3) ? 1 : 0;
}
else
{
tmp[row][col] = (aliveNeighbors==2 || aliveNeighbors==3) ? 1 : 0;
}
}
}
for(int row = 0; row < m; ++row)
{
for(int col = 0; col < n; ++col)
{
board[row][col] = tmp[row][col];
}
}
}
private int countLiveNeighbors(int[][] board, int row, int col)
{
int m = board.length;
int n = board[0].length;
int live = 0;
int[] offsets = {-1, 0, 1};
for(int dr : offsets)
{
for(int dc : offsets)
{
if(dr==0 && dc==0)
{
continue;
}
int neighborRow = row+dr;
int neighborCol = col+dc;
if(neighborRow >= 0
&& neighborRow < m
&& neighborCol >= 0
&& neighborCol < n
&& board[neighborRow][neighborCol]==1)
{
++live;
}
}
}
return live;
}
}
[code]
Runtime: 0 ms, faster than 100.00% of Java online submissions for Game of Life. Memory Usage: 37.1 MB, less than 84.24% of Java online submissions for Game of Life.
Idea – 2
A cell has two possible current states (alive and dead) and two possible next states. We can represent these four cases with four different numbers other than 0 and 1, and update the input table in place. Later we will do another pass to convert those numbers into 0 and 1.
| Current State |
Next State |
Code |
| 0 |
0 |
-2 |
| 0 |
1 |
2 |
| 1 |
0 |
-3 |
| 1 |
1 |
3 |
Time complexity is

and space is

.
[code lang="java"]
class Solution {
public void gameOfLife(int[][] board) {
if(board==null || board.length==0)
{
return;
}
int m = board.length;
int n = board[0].length;
// (0, 0) : -2
// (0, 1) : 2
// (1, 0) : -3
// (1, 1) : 3
for(int row = 0; row < m; ++row)
{
for(int col = 0; col < n; ++col)
{
int aliveNeighbors = countLiveNeighbors(board, row, col);
if(Math.abs(board[row][col])%2==0) // current state == dead
{
board[row][col] = (aliveNeighbors==3) ? 2 : -2;
}
else
{
board[row][col] = (aliveNeighbors==2 || aliveNeighbors==3) ? 3 : -3;
}
}
}
for(int row = 0; row < m; ++row)
{
for(int col = 0; col < n; ++col)
{
board[row][col] = board[row][col]<0 ? 0 : 1;
}
}
}
private int countLiveNeighbors(int[][] board, int row, int col)
{
int m = board.length;
int n = board[0].length;
int live = 0;
int[] offsets = {-1, 0, 1};
for(int dr : offsets)
{
for(int dc : offsets)
{
if(dr==0 && dc==0)
{
continue;
}
int neighborRow = row+dr;
int neighborCol = col+dc;
if(neighborRow >= 0
&& neighborRow < m
&& neighborCol >= 0
&& neighborCol < n
&& Math.abs(board[neighborRow][neighborCol])%2==1)
{
++live;
}
}
}
return live;
}
}
[code]
Runtime: 0 ms, faster than 100.00% of Java online submissions for Game of Life.Memory Usage: 37.1 MB, less than 91.99% of Java online submissions for Game of Life.
To represent an infinite board, we could store coordinates of the cells that are alive in a HashSet. If a cell dies in the next update that gets removed from the hash set, if a cell becomes alive then it gets added to the hash set. Integer values are bounded by [Integer.MIN_VALUE, Integer.MAX_VALUE], which causes problem for an infinite board. We may think the hash set implicitly represents a m x n board where m is the difference between the smallest y coordinate and the largest y coordinate. and n is the difference between the smallest x coordinate and the largest x coordinate.