Game of Life - Problem
Conway's Game of Life is a fascinating cellular automaton that simulates the evolution of life on a grid! ๐งฌ
You're given an
๐น Under-population: Any live cell with fewer than 2 live neighbors dies
๐น Survival: Any live cell with 2 or 3 live neighbors survives to the next generation
๐น Over-population: Any live cell with more than 3 live neighbors dies
๐น Reproduction: Any dead cell with exactly 3 live neighbors becomes alive
Important: All changes happen simultaneously - you must calculate the next state based on the current state, not on partially updated cells. Your task is to update the board in-place to reflect the next generation.
You're given an
m ร n grid where each cell is either alive (represented by 1) or dead (represented by 0). Each cell interacts with its eight neighbors (horizontally, vertically, and diagonally adjacent cells) according to these four simple rules:๐น Under-population: Any live cell with fewer than 2 live neighbors dies
๐น Survival: Any live cell with 2 or 3 live neighbors survives to the next generation
๐น Over-population: Any live cell with more than 3 live neighbors dies
๐น Reproduction: Any dead cell with exactly 3 live neighbors becomes alive
Important: All changes happen simultaneously - you must calculate the next state based on the current state, not on partially updated cells. Your task is to update the board in-place to reflect the next generation.
Input & Output
example_1.py โ Basic Evolution
$
Input:
board = [[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]]
๐ก Note:
The center forms a 'blinker' pattern that oscillates. Cell (1,1) dies from under-population (only 1 neighbor), while cells with exactly 3 neighbors come to life through reproduction.
example_2.py โ Still Life
$
Input:
board = [[1,1],[1,1]]
โบ
Output:
[[1,1],[1,1]]
๐ก Note:
This is a 'block' - a still life pattern. Each cell has exactly 2 or 3 live neighbors, so all cells survive to the next generation unchanged.
example_3.py โ Edge Case
$
Input:
board = [[1]]
โบ
Output:
[[0]]
๐ก Note:
A single live cell has 0 neighbors, so it dies from under-population. Edge case showing how isolated cells cannot survive.
Constraints
- m == board.length
- n == board[i].length
- 1 โค m, n โค 25
- board[i][j] is 0 or 1
- Follow-up: Can you solve it in-place with O(1) extra memory?
Visualization
Tap to expand
Understanding the Visualization
1
Survey the Population
Count how many living neighbors each cell has in all 8 directions
2
Apply Natural Laws
Determine fate based on Game of Life rules: loneliness, survival, overcrowding, or reproduction
3
Simultaneous Evolution
All changes happen at once - like a synchronized biological process
4
Next Generation
The board now represents the evolved state of the cellular automaton
Key Takeaway
๐ฏ Key Insight: The magic happens when we realize we can encode both current and future states in the same memory location using bit manipulation, achieving true in-place evolution!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code