forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOfLife.java
58 lines (48 loc) · 2.05 KB
/
GameOfLife.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
public class GameOfLife {
//Time complexity: O(M*N) where m and n are the matrix dimensions
//Space complexity: O(M*N) maintained an additional matrix to capture the live neighbours for each index
public void gameOfLife(int[][] board) {
int[][] countOfLive = new int[board.length][board[0].length];
for(int row = 0; row < board.length ; row++) {
for( int col = 0; col < board[0].length; col++){
int liveCount = 0;
int currRow = row;
//current row
for(int currCol = col - 1; currCol <= col + 1; currCol++){
if(currCol >= 0 && currCol < board[0].length && currCol != col) {
liveCount+= board[currRow][currCol];
}
}
//previous row
if(row > 0) {
currRow = row - 1;
for(int currCol = col - 1; currCol <= col + 1; currCol++){
if(currCol >= 0 && currCol < board[0].length) {
liveCount+= board[currRow][currCol];
}
}
}
//next row
if(row < board.length - 1) {
currRow = row + 1;
for(int currCol = col - 1; currCol <= col + 1; currCol++){
if(currCol >= 0 && currCol < board[0].length) {
liveCount+= board[currRow][currCol];
}
}
}
countOfLive[row][col] = liveCount;
}
}
for(int row = 0; row < board.length ; row++) {
for( int col = 0; col < board[0].length; col++) {
if(board[row][col] == 0 && countOfLive[row][col] == 3){
board[row][col] = 1;
}
if(board[row][col] == 1 && (countOfLive[row][col] < 2 || countOfLive[row][col] > 3)){
board[row][col] = 0;
}
}
}
}
}