dsa · medium
Game of Life
board is an m × n grid of 0 (dead) and 1 (live). Update it one simultaneous generation of Conway's Game of Life and return the board. Each cell looks at its eight neighbors (cells outside the grid do not count). All cells use the **previous** generation:
Arguments
board— m×n grid of 0 (dead) and 1 (live) cells to update in place
- a live cell with fewer than two live neighbors dies
- a live cell with two or three live neighbors stays live
- a live cell with more than three live neighbors dies
- a dead cell with exactly three live neighbors becomes live
Example
board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]].
The live cells are (0,1), (1,2), (2,0), (2,1), (2,2). After one tick the board is [[0,0,0],[1,0,1],[0,1,1],[0,1,0]].
Constraints
1 <= m, n <= 25 board[i][j] is 0 or 1 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [[0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 0]] Expected: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Example 2
Input: [[1, 1], [1, 0]] Expected: [[1,1],[1,1]]