289. Game of Life

Game of Life (LeetCode 289) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Set Matrix Zeroes (In-place Marking) subpattern.

How to solve Game of Life

Game of Life is a Set Matrix Zeroes In-Place problem, part of Array/Matrix Manipulation Patterns. Learn the in-place marking technique to set entire rows and columns to zero efficiently without extra space.

Work towards the solution in these steps:

  1. Think about how you can iterate over each cell in the 2D grid and analyze its neighbors to determine its next state.
  2. Consider that each cell has up to eight neighbors; you need to count how many of these neighbors are alive to apply the Game of Life rules correctly.
  3. Since the update must happen simultaneously, think about how you can store both the current and next states within the same board without losing information.

The reference solution runs in O(m * n) time and O(1) space.

The Game of Life solution and editorial walks through the full approach with reference code.

Problem statement

According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): Any live cell with fewer than two live neighbors dies as if caused by under-population.

Any live cell with two or three live neighbors lives on to the next generation.

Any live cell with more than three live neighbors dies, as if by over-population.

Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.

Example 1:

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

Example 2:

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

Constraints:

- m == board.length - n == board[i].length - 1 <= m, n <= 25 - board[i][j] is 0 or 1.

Follow up: Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.

In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?

Topics and companies

Topics: Array.

Reported in interviews at Dropbox, Amazon, Reddit, Opendoor, Microsoft, Square.

How to practise Game of Life on Thita.ai

Starter code is provided in C, C#, C++, dart, Go, Java, JavaScript, Kotlin, PHP, Python, python3, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m * n) time and O(1) space.

Open Game of Life in the code editor, or read the Game of Life editorial for a worked solution with its approach and complexity analysis.

Where Game of Life sits in the DSA pattern sheet

Problems related to Game of Life

Other problems that use the same Set Matrix Zeroes (In-place Marking) and Array/Matrix Manipulation Patterns techniques:

Game of Life FAQ

What is the LeetCode number of Game of Life?

Game of Life is LeetCode problem 289, rated Medium.

What pattern is used to solve Game of Life?

Game of Life is a Set Matrix Zeroes In-Place problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Set Matrix Zeroes (In-place Marking) subpattern.

What is the time complexity of Game of Life?

The reference solution runs in O(m * n) time and O(1) space.

Which companies ask Game of Life?

Game of Life has been reported in interviews at Dropbox, Amazon, Reddit, Opendoor, Microsoft, Square.

Preparing this workspace.