289. Game of Life (Medium)

https://leetcode.com/problems/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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.

Example:

Input: 
[
  [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]
]

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?

Solutions

class Solution {
    public void gameOfLife(int[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) {
            return;
        }

        int[] R = new int[]{-1, -1, -1, 0, 0, 1, 1, 1};
        int[] C = new int[]{1, 0, -1, 1, -1, 1, 0, -1};

        int row = board.length;
        int col = board[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                int liveNeigh = 0;

                for (int d = 0; d < 8; d++) {
                    int r = i + R[d];
                    int c = j + C[d];

                    if (r < 0 || c < 0 || r >= row || c >= col) {
                        continue;
                    }

                    // Value Integer.MIN_VALUE is converted from 1, MIN_VALUE also represents 1 in
                    // the old board. Similarly, this also applies to MAX_VALUE which represents
                    // the old 0

                    if (board[r][c] == 1 || board[r][c] == Integer.MIN_VALUE) {
                        liveNeigh++;
                    }
                }

                if (board[i][j] == 0) {

                    // Any dead cell with exactly three live neighbors becomes a live cell,
                    // as if by reproduction.
                    if (liveNeigh == 3) {
                        board[i][j] = Integer.MAX_VALUE;
                    }

                } else if (board[i][j] == 1) {

                    // Any live cell with two or three live neighbors lives on to the
                    // next generation.
                    if (liveNeigh <= 3 && liveNeigh >= 2) {
                        continue;
                    }

                    // Any live cell with more than three live neighbors dies, as if by
                    // over-population..
                    if (liveNeigh > 3) {
                        board[i][j] = Integer.MIN_VALUE;
                        continue;
                    }

                    // Any live cell with fewer than two live neighbors dies, as if caused
                    // by under-population.
                    if (liveNeigh < 2) {
                        board[i][j] = Integer.MIN_VALUE;
                    }
                }
            }
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (board[i][j] == Integer.MIN_VALUE) {
                    board[i][j] = 0;
                } else if (board[i][j] == Integer.MAX_VALUE) {
                    board[i][j] = 1;
                }
            }
        }
    }
}

Incorrect Solutions

References

Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-12-03 11:01:18

results matching ""

    No results matching ""