-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathProblem3.java
More file actions
50 lines (47 loc) · 1.46 KB
/
Problem3.java
File metadata and controls
50 lines (47 loc) · 1.46 KB
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
class Solution {
public void gameOfLife(int[][] board) {
int m=board.length;
int n=board[0].length;
//first pass
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int count = countAlive(board, i ,j);
//1-->0 maps to 2
if(board[i][j]==1 && (count<2 || count>3)){
board[i][j]=2;
}
//0-->1 maps to 3
if(board[i][j]==0 && count==3 ){
board[i][j]=3;
}
}
}
//second pass
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
//2 maps backs to 0
if(board[i][j]==2){
board[i][j]=0;
}
//3 maps back to 1
if(board[i][j]==3){
board[i][j]=1;
}
}
}
}
private int countAlive(int[][] board, int i, int j){
int count=0; // up down right left
int dirs[][]= new int [][] {{0,1},{0,-1},{1,0},{-1,0},{-1,1},{1,1},{-1,-1},{1,-1}};
for(int[] dir: dirs){
//nr= nieghbouring row , nc=nieghbouring column
int nr=i+dir[0];
int nc=j+dir[1];
if(nr>=0 && nc>=0 && nr<board.length && nc<board[0].length &&
(board[nr][nc]==1 || board[nr][nc]==2)){
count++;
}
}
return count;
}
}