bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 289. Game of Life

289. Game of Life

Leetcode Solutions

In-Place Solution with State Encoding

  1. Iterate through each cell in the board.
  2. Count the live neighbors for each cell.
  3. Apply the rules of the game to determine the next state:
    • If a live cell has fewer than two or more than three live neighbors, set its value to 2 (live to dead).
    • If a dead cell has exactly three live neighbors, set its value to 3 (dead to live).
    • Otherwise, keep the current state.
  4. Iterate through the board again and update the cells to their new state:
    • If a cell's value is 2, set it to 0 (dead).
    • If a cell's value is 3, set it to 1 (live).
    • Otherwise, keep the current state.
UML Thumbnail

Extra Memory Solution with Copy of the Board

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...