September 06, 2019

289. Game of Life

289. Game of Life

Sol 1. Naive

Time = O(mn), space = O(mn)

class Solution {
    public void gameOfLife(int[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) {
            return;
        }
        int[][] newBoard = new int[board.length][board[0].length];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                int numOfLife = getNumOfLife(i, j, board);
                if (board[i][j] == 1) {
                    if (numOfLife < 2 || numOfLife > 3) {
                        newBoard[i][j] = 0;
                    } else {
                        newBoard[i][j] = 1;
                    } 
                } else {
                    if (numOfLife == 3) {
                        newBoard[i][j] = 1;
                    } else {
                        newBoard[i][j] = 0;
                    }
                }
            }
        }
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] = newBoard[i][j];
            }
        }
    }
    
    private int getNumOfLife(int i, int j, int[][] board) {
        int numOfLife = 0;
        for (int x = Math.max(0, i - 1); x < Math.min(i + 2, board.length); x++) {
            for (int y = Math.max(0, j - 1); y < Math.min(j + 2, board[0].length); y++) {
                if (board[x][y] == 1) {
                    numOfLife ++;
                }
            }
        }
        if (board[i][j] == 1) {
            numOfLife --;
        }
        return numOfLife;
    }
}

Sol 2. Bit manipulation - Space reduction

Time = O(mn), space = O(1)

A useful post: https://leetcode.com/problems/game-of-life/discuss/73223/Easiest-JAVA-solution-with-explanation

class Solution {
    public void gameOfLife(int[][] board) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                int numOfLife = getNumOfLife(i, j, board);
                
                if (board[i][j] == 1) { // live 
                    if (numOfLife < 2 || numOfLife > 3) {
                        board[i][j] = 1; // 01 now - prev
                    } else {
                        board[i][j] = 3; // 11
                    }
                } else { // dead
                    if (numOfLife == 3) {
                        board[i][j] = 2; // 10
                    } 
                }
            }
        }
        
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] >>= 1;
            }
        }   
    }
    
    private int getNumOfLife(int i, int j, int[][] board) {
        int numOfLife = 0;
        for (int x = Math.max(i - 1, 0); x < Math.min(i + 2, board.length); x++) {
            for (int y = Math.max(j - 1, 0); y < Math.min(j + 2, board[0].length); y++) {
                numOfLife += board[x][y] & 1;
            }
        }
        numOfLife -= board[i][j] & 1;
        return numOfLife;
    }
    
}
comments powered by Disqus