Game of Life: Next Generation
Problem statement
Given a rectangular grid board of dead cells (0) and live cells (1), return its state after one simultaneous Game of Life generation.
Each cell has up to eight neighbors: horizontal, vertical, and diagonal cells within the grid. A live cell survives with two or three live neighbors and otherwise dies. A dead cell becomes live with exactly three live neighbors and otherwise stays dead. Count all neighbors from the original grid before any update.
For this exercise, the grid is finite: cells outside it are dead. Return the complete next-state grid in the same dimensions.
Function
gameOfLife(board: int[][]) → int[][]Examples
Example 1
board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]return = [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]Apply the neighbor counts to the original four-row grid at the same time.
Example 2
board = [[1,1],[1,0]]return = [[1,1],[1,1]]The dead lower-right cell has three live neighbors and becomes live; the others each have two neighbors and survive.
Example 3
board = [[0,0,0],[1,1,1],[0,0,0]]return = [[0,1,0],[0,1,0],[0,1,0]]The three-cell horizontal line becomes a vertical line because updates occur simultaneously.
Constraints
1 <= board.length, board[i].length <= 25.- Every row has the same length.
- Every cell is
0or1.