|
1 | 1 | package com.interview.multiarray;
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * Date 10/20/2016 |
| 5 | + * @author Tushar Roy |
| 6 | + * Given a board with m by n cells, each cell has an initial state live (1) or dead (0). |
| 7 | + * Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following |
| 8 | + * four rules (taken from the above Wikipedia article): |
| 9 | + * Read full qs on leetcode. |
| 10 | + * |
| 11 | + * Solution - Keep two array prev and current. Fill the values in current array. As soon as current row is done |
| 12 | + * replace elemments of board with prev array. |
| 13 | + * |
| 14 | + * Time complexity O(n * m) |
| 15 | + * |
| 16 | + * https://leetcode.com/problems/game-of-life/ |
| 17 | + */ |
3 | 18 | public class GameOfLife {
|
| 19 | + public void gameOfLife(int[][] board) { |
| 20 | + if (board.length == 0 || board[0].length == 0) { |
| 21 | + return; |
| 22 | + } |
| 23 | + int n = board.length; |
| 24 | + int m = board[0].length; |
| 25 | + int[] prevRow = new int[m]; |
| 26 | + int[] currentRow = new int[m]; |
4 | 27 |
|
5 |
| - boolean [][]board = null; |
6 |
| - boolean [][]tempBoard = null; |
7 |
| - public GameOfLife(boolean[][] initialState){ |
8 |
| - board = initialState; |
9 |
| - tempBoard = new boolean[board.length][board.length]; |
10 |
| - } |
11 |
| - |
12 |
| - public void printState(){ |
13 |
| - for(int i=0; i < board.length; i++){ |
14 |
| - for(int j=0; j < board[i].length; j++){ |
15 |
| - if(board[i][j]){ |
16 |
| - System.out.print("1 "); |
17 |
| - }else{ |
18 |
| - System.out.print("0 "); |
19 |
| - } |
20 |
| - } |
21 |
| - System.out.print("\n"); |
22 |
| - } |
23 |
| - System.out.print("\n\n"); |
24 |
| - } |
25 |
| - |
26 |
| - public void next(){ |
27 |
| - |
28 |
| - int count=0; |
29 |
| - for(int i=0; i < board.length; i++){ |
30 |
| - for(int j=0; j < board[i].length; j++){ |
31 |
| - count = countNeighbors(i, j); |
32 |
| - tempBoard[i][j] = board[i][j]; |
33 |
| - if(count <= 1){ |
34 |
| - tempBoard[i][j] = false; |
35 |
| - } |
36 |
| - if(count ==3){ |
37 |
| - tempBoard[i][j] = true; |
38 |
| - } |
39 |
| - if(count >= 4){ |
40 |
| - tempBoard[i][j] = false; |
41 |
| - } |
42 |
| - } |
43 |
| - } |
44 |
| - boolean[][] rBoard = tempBoard; |
45 |
| - tempBoard = board; |
46 |
| - board = rBoard; |
47 |
| - } |
| 28 | + for (int i = 0; i < n; i++) { |
| 29 | + for (int j = 0; j < m; j++) { |
| 30 | + currentRow[j] = doesLive(i, j, board) ? 1 : 0; |
| 31 | + } |
| 32 | + if (i != 0) { |
| 33 | + copyRow(prevRow, board[i - 1]); |
| 34 | + } |
| 35 | + if (i != n - 1) { |
| 36 | + copyRow(currentRow, prevRow); |
| 37 | + } |
| 38 | + } |
| 39 | + copyRow(currentRow, board[n - 1]); |
| 40 | + } |
48 | 41 |
|
49 |
| - public void nextOptimized(){ |
50 |
| - |
51 |
| - boolean temp1[] = new boolean[board[0].length]; |
52 |
| - boolean temp2[] = new boolean[board[0].length]; |
53 |
| - calculate(board,temp1,0); |
54 |
| - for(int i=1; i < board.length; i++){ |
55 |
| - calculate(board,temp2,i); |
56 |
| - copy(i-1,temp1); |
57 |
| - copy(temp1,temp2); |
58 |
| - } |
59 |
| - copy(board.length-1,temp1); |
60 |
| - } |
61 |
| - |
62 |
| - void copy(boolean arr1[],boolean arr2[]){ |
63 |
| - for(int i=0; i <arr2.length; i++){ |
64 |
| - arr1[i] = arr2[i]; |
65 |
| - } |
66 |
| - } |
67 |
| - |
68 |
| - void calculate(boolean [][]board,boolean temp[],int i){ |
69 |
| - int count=0; |
70 |
| - for(int j=0; j < board[i].length; j++){ |
71 |
| - count = countNeighbors(i, j); |
72 |
| - temp[j] = board[i][j]; |
73 |
| - if(count <= 1){ |
74 |
| - temp[j] = false; |
75 |
| - } |
76 |
| - if(count ==3){ |
77 |
| - temp[j] = true; |
78 |
| - } |
79 |
| - if(count >= 4){ |
80 |
| - temp[j] = false; |
81 |
| - } |
82 |
| - } |
| 42 | + private void copyRow(int[] source, int[] dest) { |
| 43 | + for (int i = 0; i < source.length; i++) { |
| 44 | + dest[i] = source[i]; |
| 45 | + } |
| 46 | + } |
83 | 47 |
|
84 |
| - } |
85 |
| - |
86 |
| - private void copy(int i,boolean []temp){ |
87 |
| - for(int x=0; x < temp.length; x++){ |
88 |
| - board[i][x] = temp[x]; |
89 |
| - } |
90 |
| - } |
91 |
| - |
92 |
| - private int countNeighbors(int i,int j){ |
93 |
| - int count =0; |
94 |
| - for(int k = i-1; k <= i+1; k++){ |
95 |
| - for(int l = j-1; l <= j+1; l++){ |
96 |
| - if((i ==k && j == l) || k < 0 || l < 0 || k >= board.length || l >= board[k].length){ |
97 |
| - continue; |
98 |
| - } |
99 |
| - if(board[k][l]){ |
100 |
| - count++; |
101 |
| - } |
102 |
| - } |
103 |
| - } |
104 |
| - return count; |
105 |
| - } |
106 |
| - |
107 |
| - public static void main(String args[]){ |
108 |
| - boolean[][] initialState = new boolean[10][10]; |
109 |
| - initialState[3][6] = true; |
110 |
| - initialState[4][6] = true; |
111 |
| - initialState[5][6] = true; |
112 |
| - initialState[5][7] = true; |
113 |
| - initialState[5][8] = true; |
114 |
| - GameOfLife gol = new GameOfLife(initialState); |
115 |
| - gol.printState(); |
116 |
| - gol.nextOptimized(); |
117 |
| - gol.printState(); |
118 |
| - gol.nextOptimized(); |
119 |
| - gol.printState(); |
120 |
| - gol.nextOptimized(); |
121 |
| - gol.printState(); |
122 |
| - gol.nextOptimized(); |
123 |
| - gol.printState(); |
124 |
| - |
125 |
| - } |
| 48 | + private boolean doesLive(int x, int y, int[][] board) { |
| 49 | + int count = 0; |
| 50 | + for (int i = x - 1; i <= x + 1; i++) { |
| 51 | + for (int j = y - 1; j <= y + 1; j++) { |
| 52 | + if (x == i && y == j) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + if (i < 0 || i >= board.length) { |
| 56 | + break; |
| 57 | + } |
| 58 | + if (j < 0 || j >= board[0].length) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + count += board[i][j]; |
| 62 | + } |
| 63 | + } |
| 64 | + if (board[x][y] == 1) { |
| 65 | + return count == 2 || count == 3; |
| 66 | + } else { |
| 67 | + return count == 3; |
| 68 | + } |
| 69 | + } |
126 | 70 | }
|
0 commit comments