Problem Description
The rules of the game are described in detail in the Wikipedia article.
The board is an m x n grid of cells, where each cell has an initial state:
- live: denoted by
1 - dead: denoted by
0.
Each cell interacts with its eight neighbours (horizontal, vertical, diagonal) using the following four rules.
- Any live cell with fewer than two live neighbours dies, as if by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The next state of the board is created by applying the rules above to every cell of the current state simultaneously, so births and deaths happen at the same time.
Write a function that takes the board state and updates it to the next state.
Constraints
m == board.lengthn == board[i].length1 <= m, n <= 25board[i][j] == 0 || board[i][j] == 1
Examples
Input
[
[0, 1, 0],
[0, 0, 1],
[1, 1, 1],
[0, 0, 0],
]Answer
[
[0, 0, 0],
[1, 0, 1],
[0, 1, 1],
[0, 1, 0],
]Solution
This problem is very easy to solve if we write the changes into a copy of the matrix. That keeps the algorithm simple, but requires additional memory, which can become a problem with large inputs.
To keep memory usage optimal, we need to make all changes in place. But if we simply change the state in the original matrix while checking each cell, we lose the original state and can no longer correctly decide whether other cells should change.
To work around this, we introduce two new values for the matrix.
2marks a transition from zero to one. We use it to mark dead cells that should become alive as a result of our computations.3marks a transition from one to zero. We use it to mark live cells that should die as a result of our computations.
These new values must be taken into account when computing the state of each cell.
At the end, after updating the states of all cells, we go over the matrix once more and replace every 2 with 1 and every 3 with 0.
export const gameOfLife = (board: number[][]): void => {
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
const count = liveCount(board, i, j);
if (board[i][j] == 0) {
if (count == 3) {
board[i][j] = 2;
}
continue;
}
if (count < 2 || count > 3) {
board[i][j] = 3;
continue;
}
board[i][j] = 1;
}
}
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] == 3) {
board[i][j] = 0;
}
if (board[i][j] == 2) {
board[i][j] = 1;
}
}
}
};
function liveCount(board: number[][], i: number, j: number): number {
const m = board[i].length;
const n = board.length;
let up = 0;
let down = 0;
let sides = 0;
if (i > 0) {
up = getValue(board[i - 1][j]);
if (j > 0) {
up += getValue(board[i - 1][j - 1]);
}
if (j < m - 1) {
up += getValue(board[i - 1][j + 1]);
}
}
if (i < n - 1) {
down = getValue(board[i + 1][j]);
if (j > 0) {
up += getValue(board[i + 1][j - 1]);
}
if (j < m - 1) {
up += getValue(board[i + 1][j + 1]);
}
}
if (j > 0) {
sides += getValue(board[i][j - 1]);
}
if (j < m - 1) {
sides += getValue(board[i][j + 1]);
}
return up + down + sides;
}
function getValue(val: number): number {
if (val == 2) {
return 0;
}
if (val == 3) {
return 1;
}
return val;
}Complexity Analysis
Time Complexity
O(m * n), since we go over the whole matrix cell by cell.
Space Complexity
O(1), since we don't allocate additional memory.