235. Game of Life

Medium
In-place State Encoding
Array

Problem

Given an m x n binary board representing cells in Conway's Game of Life, compute and return the board after one simultaneous update. A live cell survives with two or three live neighbors, while a dead cell becomes live with exactly three live neighbors.

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

Example 2

Input: [[1,1],[1,0]]
Output: [[1,1],[1,1]]

Example 3

Input: [[0]]
Output: [[0]]
Constraints

1 <= board.length <= 25

1 <= board[i].length <= 25

board[i][j] is either 0 or 1.

Hints

?? Every update must be based on the original board state.

?? Temporary integer states can encode both the old and new value.

?? After evaluating every cell, convert temporary states into final binary states.

Expected Complexity
Time: O(m * n)
Space: O(1) auxiliary
Follow-up

Can you compute the next generation using O(1) auxiliary board space?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
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]]