Advertisement

Game of Life - LeetCode 289 Solution

Game of Life - Complete Solution Guide

Game of Life is LeetCode problem 289, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 tw

Detailed Explanation

The Game of Life is a cellular automaton where cells in a grid evolve based on a set of rules related to their neighbors. The input is a 2D array (board) representing the current state of the game, where '1' represents a live cell and '0' represents a dead cell. The task is to update the board in-place to its next state based on these rules: 1. A live cell with fewer than two live neighbors dies (underpopulation). 2. A live cell with two or three live neighbors lives on to the next generation. 3. A live cell with more than three live neighbors dies (overpopulation). 4. A dead cell with exactly three live neighbors becomes a live cell (reproduction). The board is updated simultaneously; that is, changes to one cell shouldn't affect calculations for another cell in the same step. The constraints include the dimensions of the board being between 1 and 25, and each cell being either 0 or 1.

Solution Approach

The provided solution updates the board in-place using bit manipulation. It iterates through each cell, counts the number of live neighbors, and then uses the rules to determine the cell's next state. Instead of directly updating the cell with 0 or 1, it encodes the next state in the second bit of the cell's value. After processing all cells, it shifts the bits to the right by one position, effectively updating the board with the next state.

Step-by-Step Algorithm

  1. Step 1: Iterate through each cell (i, j) of the board.
  2. Step 2: For each cell, iterate through its eight neighbors (including diagonals). Handle boundary conditions to avoid out-of-bounds access.
  3. Step 3: Count the number of live neighbors for the current cell. Remember to only consider cells within bounds.
  4. Step 4: Apply the Game of Life rules based on the current cell's state (live or dead) and the number of live neighbors: - If the cell is live (board[i][j] & 1 == 1): - If live_neighbors is 2 or 3, mark the cell to live in the next state (board[i][j] |= 2). - If the cell is dead (board[i][j] & 1 == 0): - If live_neighbors is 3, mark the cell to live in the next state (board[i][j] |= 2).
  5. Step 5: After processing all cells, iterate through the board again and update each cell's value by shifting its bits to the right by one position (board[i][j] >>= 1). This effectively moves the next state (stored in the second bit) to the first bit, updating the board.

Key Insights

  • Insight 1: The core challenge is updating the board in-place while ensuring changes don't affect other cells in the same generation's calculation. Using additional space to store the next state would be straightforward but violates the in-place requirement.
  • Insight 2: The solution uses bit manipulation to store the next state alongside the current state within the same cell. This is achieved by using the 2nd bit (value 2) of each cell to represent the next state.
  • Insight 3: Careful iteration through the neighbors of each cell and boundary condition checks are crucial for correctly applying the rules.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(1)

Topics

This problem involves: Array, Matrix, Simulation.

Companies

Asked at: Anduril, BitGo, Cloudflare, Dropbox, Google, Snap, Two Sigma, Warnermedia.