Leetcode: 289. Game of Life

Question:

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:
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.
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?

Note:

The problem might look very easy at first, however, the most important catch in this problem is to realize that if you update the original array with the given rules, you won’t be able to perform simultaneous updating as is required in the question. You might end up using the updated values for some cells to update the values of other cells. But the problem demands applying the given rules simultaneously to every cell.
Thus, you cannot update some cells first and then use their updated values to update other cells.

Solution 1: O(mn) space

class Solution {
    public void gameOfLife(int[][] board) {
    	if (board == null || board.length == 0) {
    		return;
    	}
    	int rows = board.length;
    	int rols = board[0].length;
    	// make a copy of the original board
    	int[][] copyBoard = new int[rows][cols];
    	for (row = 0; row < rows; row++) {
    		for (col = 0; col < cols; col++) {
    			copyBoard[row][col] = board[row];
     		}
    	}
    	// a way to iterate the neighbors of a certain cell in the board
    	int[] neighbors = {0, 1, -1};
    	for (int row = 0; row < rows; row++) {
    		for (int col = 0; col < cols; col++) {
    			// for a certain cell, we need to count its live neighbors
    			int liveNeighbors = 0;
    			for (int i = 0; i < 3; i++) {
    				for (int j = 0; j < 3; j++) {
    					if (!(neighbors[j] == 0 && neighbors[j] == 0)) {
    						// when the position is not at itself's
    						int r = row + neighbors[i];
    						int c = col + neighbors[j];
    						if ((r >= 0 && r < rows) && (c >= 0 && c < cols) && (copyBoard[r][c] == 1)) 
    							liveNeighbors ++;
    					}
    				}
    			}
    			// Rule 1 and Rule 3
    			if (copyBoard[row][col] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) 
    				board[row][col] = 0;
    			// Rule 4
    			if (copyBoard[row][col] == 0 && liveNeighbors == 3)
    				board[row][col] = 1;
    		}
    	}
    }
}

Solution 2: O(1) space complexity

class Solution {
    public void gameOfLife(int[][] board) {
    	if (board == null || board.length == 0) {
    		return;
    	}
    	int rows = board.length;
    	int rols = board[0].length;
    	// a way to iterate the neighbors of a certain cell in the board
    	int[] neighbors = {0, 1, -1};
    	for (int row = 0; row < rows; row++) {
    		for (int col = 0; col < cols; col++) {
    			// for a certain cell, we need to count its live neighbors
    			int liveNeighbors = 0;
    			for (int i = 0; i < 3; i++) {
    				for (int j = 0; j < 3; j++) {
    					if (!(neighbors[j] == 0 && neighbors[j] == 0)) {
    						// when the position is not at itself's
    						int r = row + neighbors[i];
    						int c = col + neighbors[j];
    						if ((r >= 0 && r < rows) && (c >= 0 && c < cols) && (Math.abs(board[r][c]) == 1)) 
    							liveNeighbors ++;
    					}
    				}
    			}
    			// Rule 1 and Rule 3
    			if (board[row][col] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) 
    				board[row][col] = -1;
    			// Rule 4
    			if (board[row][col] == 0 && liveNeighbors == 3)
    				board[row][col] = 2;
    		}
    	}
    	for (int row = 0; row < rows; row++) {
    		for (int col = 0; col < cols; col++) {
    			if (board[row][col] > 0) {
                    board[row][col] = 1;
                } else {
                    board[row][col] = 0;
    		}
    	}
    }
}

Summary:

Pay attention to how to represent the “neighbors” in an 2D array.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值