loading

Game of Life — Step-by-Step Visualization

mediumLeetCode #289ArrayMatrixSimulation

The board is made up of cells (0=dead, 1=live). Apply Game of Life rules simultaneously: live cell with <2 live neighbors dies; live cell with 2-3 lives; live cell with >3 dies; dead cell with exactly 3 becomes live.

Algorithm Pattern

In-place State Encoding

Key Idea

Use temporary codes (-1, 2) to record transitions without a copy, then finalize in a second pass.

Step-by-Step Approach

  1. For each cell, count live neighbors (treat |val|==1 as live)
  2. Mark dying cells as -1, born cells as 2
  3. Second pass: -1→0, 2→1

Related Problems