DSA · Matrix · #77
Game of Life
Module 63 · difficulty 3/5·⏱ 30:00starts on first keystroke
Implement `gameOfLife(board)`. The `board` is an `m x n` grid of integers where `1` is a **live** cell and `0` is a **dead** cell. Compute the next state by applying these rules simultaneously to every cell, using its eight neighbors: 1. A live cell with fewer than two live neighbors dies (under-population). 2. A live cell with two or three live neighbors lives on. 3. A live cell with more than three live neighbors dies (over-population). 4. A dead cell with exactly three live neighbors becomes live (reproduction). The next state is created by applying the rules to the **current** state of all cells at once. Modify `board` **in-place** and return nothing.
Examples
board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]→[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]— Classic glider; all cells transition simultaneously.board = [[1,1],[1,0]]→[[1,1],[1,1]]— The dead bottom-right cell has exactly three live neighbors, so it becomes live.board = [[0,1,0],[0,1,0],[0,1,0]]→[[0,0,0],[1,1,1],[0,0,0]]— Blinker oscillates from vertical to horizontal.
Constraints
- · m == board.length
- · n == board[i].length
- · 1 <= m, n <= 25
- · board[i][j] is 0 or 1
- · Solve it in-place; the optimal solution uses O(1) extra space.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.