Given a grid grid of 0 (dead) and 1 (live) cells, return a new grid holding the next
generation under these rules, where a cell's neighbours are the up-to-eight cells touching it:
- a live cell with 2 or 3 live neighbours stays live; otherwise it dies,
- a dead cell with exactly 3 live neighbours becomes live; otherwise it stays dead.
Cells outside the grid are always dead.
Examples
Input: grid = [[0, 1, 0],
[0, 1, 0],
[0, 1, 0]]
Output: [[0, 0, 0],
[1, 1, 1],
[0, 0, 0]]
Input: grid = [[1, 1],
[1, 1]]
Output: [[1, 1],
[1, 1]]
Constraints
1 <= rows, cols <= 100- The input grid must not be modified.
Goals
- Count live neighbours in all eight directions
- Compute the next state from the old state without corrupting it