[LeetCode] 289. Game of Life

According to the 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."
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.
Follow up
  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. 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 the border of the array. How would you address these problems?

Thought process:
Currently a cell can either be 0 or 1. We can give an extra bit to the cells to keep track of their next states. After iterating through the board, right shift all cells by 1 bit.
When a cell is on the border of the matrix, it has fewer cells than the ones in the center.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
    public void gameOfLife(int[][] board) {
        int m = board.length;
        if (m == 0) {
            return;
        }
        int n = board[0].length;
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int live = getLiveNeighbors(board, i, j);
                if (board[i][j] == 0 && live == 3) {
                    board[i][j] = 2;
                }
                if (board[i][j] == 1 && (live == 2 || live == 3)) {
                    board[i][j] = 3;
                }
            }
        }
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] >>= 1;
            }
        }
    }
    
    private int getLiveNeighbors(int[][] board, int row, int col) {
        int live = 0;
        int m = board.length;
        int n = board[0].length;
        
        for (int i = Math.max(row - 1, 0); i <= Math.min(row + 1, m - 1); i++) {
            for (int j = Math.max(col - 1, 0); j <= Math.min(col + 1, n - 1); j++) {
                if (i == row && j == col) {
                    continue;
                }
                if (board[i][j] % 2 == 1) {
                    live++;
                }
            }
        }
        return live;
    }
}

Time complexity: O(mn).

Comments

Popular posts from this blog

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee