#289

Game of Life

Medium In-place state encoding

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Compute the next generation of Conway's Game of Life on a grid, in place. All cells update at the same time based on their eight neighbours.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: compute the next generation, where every cell updates at the same time.

Why this works: keep the old state in bit 0 and write the new state into bit 1. Neighbours read bit 0 only; at the end shift every cell right.

Example: live cell with 1 live neighbour

old bit 1, dies → new bit 0 → value stays 01after shifting → 0 (dead)

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Updating cells directly, so later cells see a mix of old and new neighbours.

Solve on LeetCode ↗
Complexity
  • Time: O(m·n).
  • Space: O(1).
Topic: 2D Arrays & Matrix
🎬 Episode 14: video coming soon