204. Game of Life

MediumMatrix

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: 1. Any live cell with fewer than two live neighbors dies. 2. Any live cell with two or three live neighbors lives on. 3. Any live cell with more than three live neighbors dies. 4. Any dead cell with exactly three live neighbors becomes a live cell. The next state is created by applying the above rules simultaneously to every cell in the current state. Given the current state of the board, update the board to reflect its next state.

Examples

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]]

Explanation: Applying the four rules simultaneously to every cell.

Constraints

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 25
  • board[i][j] is 0 or 1.
Loading...

Run checks all cases above. Submit evaluates all test cases.