1+ M
2+ 1521012714
3+ tags : Array
4+
5+ #### basic
6+ - 简单的implementation , 把count function写清楚就好 .
7+ - time : O (mn ), extra space : O (mn )
8+ - 注意结尾要一个个board [i ][j ]copy
9+
10+ #### follow up
11+ unlimited border ? 可能需要分割board . 用大框分割 , 每次换行的时候 , 重复计算2行就好了 . see details below .
12+
13+ #### improvement : do it in place !
14+ - time : O (mn ), extra space : O (1 )
15+ - bit manipulation : 用第二个bit来存previous value .
16+ - 因为我们只考虑 0 和 1 而已 , 所以可以这样取巧 . 但是并不scalable .
17+ - 如果需要存multiple state , 可能需要移动更多位 , 或者用一个 state map
18+ - 注意 bit manipulation 的细节 : <<1 , >>1 , 还有 mast的用法 : |, &
19+
20+
21+ ```
122/*
2- According to the Wikipedia's article:
3- "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
23+ According to the Wikipedia's article: "The Game of Life, also known simply as Life,
24+ is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
425
526Given a board with m by n cells, each cell has an initial state live (1) or dead (0).
6- Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules
7- (taken from the above Wikipedia article):
27+ Each cell interacts with its eight neighbors (horizontal, vertical, diagonal)
28+ using the following four rules (taken from the above Wikipedia article):
829
930Any live cell with fewer than two live neighbors dies, as if caused by under-population.
1031Any live cell with two or three live neighbors lives on to the next generation.
@@ -16,27 +37,158 @@ Write a function to compute the next state (after one update) of the board given
1637Could you solve it in-place? Remember that the board needs to be updated at the same time:
1738You cannot update some cells first and then use their updated values to update other cells.
1839In this question, we represent the board using a 2D array.
19- In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array.
20- How would you address these problems?
40+ In principle, the board is infinite, which would cause problems
41+ when the active area encroaches the border of the array. How would you address these problems?
2142Credits:
22- Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
23-
24- Hide Company Tags Google TinyCo
25- Hide Tags Array
26- Hide Similar Problems (M) Set Matrix Zeroes
2743
2844*/
2945
3046/*
31- Thoughts:
32- https://segmentfault.com/a/1190000003819277
33- http://my.oschina.net/Tsybius2014/blog/514447
34- build state machine.
35- take mod of 2 at the end.
47+ Thoughts
48+ 1:
49+ < 2 => 0 => STATE = 2
50+ > 3 => 0 => STATE = 2
51+ ==2, ==3 => 1 => 1
52+
53+ O(mn)
3654*/
3755
38- public class Solution {
56+ // time O(mn * 8) = O(mn), Space(mn)
57+ class Solution {
58+ public void gameOfLife (int [][] board ) {
59+ if (board == null || board .length == 0 || board [0 ] == null || board [0 ].length ==0 ) {
60+ return ;
61+ }
62+ int m = board .length ;
63+ int n = board [0 ].length ;
64+ int [][] newBoard = new int [m ][n ];
65+
66+ for (int i = 0 ; i < m ; i ++) {
67+ for (int j = 0 ; j < n ; j ++) {
68+ int countNeighbor = count (board , i , j );
69+ if (board [i ][j ] == 1 ) {
70+ newBoard [i ][j ] = (countNeighbor < 2 || countNeighbor > 3 ) ? 0 : 1 ;
71+ } else { // board[i][j] == 0
72+ newBoard [i ][j ] = count (board , i , j ) == 3 ? 1 : 0 ;
73+ }
74+ }
75+ }
76+ // copy
77+ for (int i = 0 ; i < m ; i ++) {
78+ for (int j = 0 ; j < n ; j ++) {
79+ board [i ][j ] = newBoard [i ][j ];
80+ }
81+ }
82+ }
83+
84+ int [] dx = {0 , 0 , 1 , -1 , -1 , -1 , 1 , 1 };
85+ int [] dy = {1 , -1 , 0 , 0 , -1 , 1 , -1 , 1 };
86+
87+ // optimize: no need to count all.
88+ private int count (int [][] board , int x , int y ) {
89+ int count = 0 ;
90+ for (int i = 0 ; i < dx .length ; i ++) {
91+ int mX = x + dx [i ];
92+ int mY = y + dy [i ];
93+ if (mX >= 0 && mX < board .length && mY >= 0 && mY < board [0 ].length ) {
94+ count += board [mX ][mY ];
95+ }
96+ }
97+ return count ;
98+ }
99+ }
100+
101+
102+ /*
103+ Use 2nd bit as storage for previous status '0' or '1'
104+ 0 => 00
105+ 1 => 10
106+
107+ This solution is not quite scalable because it only works with '0' and '1'
108+
109+ We could implement state map if having multiple conditions.
110+ */
111+ class Solution {
39112 public void gameOfLife (int [][] board ) {
113+ if (board == null || board .length == 0 || board [0 ] == null || board [0 ].length ==0 ) {
114+ return ;
115+ }
116+ int m = board .length ;
117+ int n = board [0 ].length ;
118+
119+ // Shift: use 2nd bit to store previous condition
120+ for (int i = 0 ; i < m ; i ++) {
121+ for (int j = 0 ; j < n ; j ++) {
122+ board [i ][j ] = board [i ][j ] << 1 ;
123+ }
124+ }
40125
126+ // Count and calculate
127+ for (int i = 0 ; i < m ; i ++) {
128+ for (int j = 0 ; j < n ; j ++) {
129+ int countNeighbor = count (board , i , j );
130+
131+ // Access 2nd bit for previous value
132+ if ((board [i ][j ] >> 1 ) == 1 ) {
133+ board [i ][j ] = (countNeighbor >= 2 && countNeighbor <= 3 ) ? board [i ][j ] | 1 : board [i ][j ];
134+ } else { // board[i][j] == 0
135+ board [i ][j ] = count (board , i , j ) == 3 ? board [i ][j ] | 1 : board [i ][j ];
136+ }
137+ }
138+ }
139+
140+ // Filter out 2nd bit and only 1st bit as result
141+ for (int i = 0 ; i < m ; i ++) {
142+ for (int j = 0 ; j < n ; j ++) {
143+ board [i ][j ] = board [i ][j ] & 1 ; // remove 2nd bit, which stores previous value
144+ }
145+ }
146+ }
147+
148+ int [] dx = {0 , 0 , 1 , -1 , -1 , -1 , 1 , 1 };
149+ int [] dy = {1 , -1 , 0 , 0 , -1 , 1 , -1 , 1 };
150+
151+ // optimize: no need to count all.
152+ private int count (int [][] board , int x , int y ) {
153+ int count = 0 ;
154+ for (int i = 0 ; i < dx .length ; i ++) {
155+ int mX = x + dx [i ];
156+ int mY = y + dy [i ];
157+ if (mX >= 0 && mX < board .length && mY >= 0 && mY < board [0 ].length ) {
158+ count += (board [mX ][mY ] >> 1 );
159+ }
160+ }
161+ return count ;
41162 }
42- }
163+ }
164+
165+ /*
166+
167+ Unlimited? board太大, 一个电脑放不下, 怎么分割?
168+ 定一个大框
169+ 每次移动大框的时候, 留着重复2就行了.
170+ x x x x x x x x x x
171+ x x x x x x x x x x
172+ x x x x x x x x x x
173+ x x x x x x x x x x
174+ x x x x x x x x x x
175+ x x x x x x x x x x
176+ x x x x x x x x x x
177+ x x x x x x x x x x
178+ x x x x x x x x x x
179+ x x x x x x x x x x
180+
181+ a a a a a a a a a a
182+ y y y y y y y y y y
183+ x x x x x x x x x x
184+ x x x x x x x x x x
185+ x x x x x x x x x x
186+ x x x x x x x x x x
187+ x x x x x x x x x x
188+ x x x x x x x x x x
189+ x x x x x x x x x x
190+ x x x x x x x x x x
191+ x x x x x x x x x x
192+ x x x x x x x x x x
193+ */
194+ ```
0 commit comments