90. Game of Life

MediumArrayArray

Given an m x n grid board of 0s (dead) and 1s (live), apply one step of Conway's Game of Life rules:
1. Live cell with 2 or 3 live neighbors → lives
2. Live cell with <2 or >3 live neighbors → dies
3. Dead cell with exactly 3 live neighbors → lives

Return the next state.

Input: A 2D binary array board of size m x n.

Output: The board after one step.

Examples

Example 1
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: Apply rules to each cell simultaneously.
Example 2
Input: [[1,1],[1,0]]
Output: [[1,1],[1,1]]
Explanation: Bottom-right dead cell has 3 live neighbors→lives.
Example 3
Input: [[1]]
Output: [[0]]
Explanation: Single cell has 0 neighbors→dies.

Constraints

Asked by

AdobeMicrosoftAmazonGoogleMetaBloomberg
Solve this problem in the editor →