1+ According to Wikipedia - s article :
2+ The Game of Life , also known simply as Life ,
3+ is a cellular automaton devised by the British mathematician John Horton Conway in 1970.
4+
5+ The board is made up of an m x n grid of cells , where each cell has an initial state :
6+ live (represented by a 1 ) or dead (represented by a 0 ).
7+ Each cell interacts with its eight neighbors (horizontal , vertical , diagonal )
8+ using the following four rules (taken from the above Wikipedia article ):
9+
10+ Any live cell with fewer than two live neighbors dies as if caused by under - population .
11+ Any live cell with two or three live neighbors lives on to the next generation .
12+ Any live cell with more than three live neighbors dies , as if by over - population .
13+ Any dead cell with exactly three live neighbors becomes a live cell , as if by reproduction .
14+ The next state is created by applying the above rules simultaneously to every cell
15+ in the current state , where births and deaths occur simultaneously .
16+ Given the current state of the m x n grid board , return the next state .
17+
18+
19+
20+ Example 1 :
21+ Input : board = [[0 ,1 ,0 ],[0 ,0 ,1 ],[1 ,1 ,1 ],[0 ,0 ,0 ]]
22+ Output : [[0 ,0 ,0 ],[1 ,0 ,1 ],[0 ,1 ,1 ],[0 ,1 ,0 ]]
23+
24+ Example 2 :
25+ Input : board = [[1 ,1 ],[1 ,0 ]]
26+ Output : [[1 ,1 ],[1 ,1 ]]
27+
28+ Constraints :
29+ m == board .length
30+ n == board [i ].length
31+ 1 <= m , n <= 25
32+ board [i ][j ] is 0 or 1.
33+
34+ Follow up :
35+ Could you solve it in - place ? Remember that the board needs to be updated simultaneously : You cannot update some cells first and then use their updated values to update other cells .
36+ In this question , we represent the board using a 2 D array . In principle , the board is infinite , which would cause problems when the active area encroaches upon the border of the array (i .e ., live cells reach the border ). How would you address these problems ?
37+
38+
39+
40+ # O(m*n) Time and O(1) Space
41+ class Solution :
42+ def gameOfLife (self , board : List [List [int ]]) - > None :
43+ """
44+ Do not return anything, modify board in-place instead.
45+ """
46+
47+ self .directions = [[0 ,- 1 ], [0 ,1 ], [1 ,0 ], [- 1 , 0 ], [1 , 1 ], [1 , - 1 ], [- 1 , 1 ], [- 1 , - 1 ]]
48+
49+ self .board = board
50+
51+ self .m = len (board )
52+ self .n = len (board [0 ])
53+
54+
55+ for i in range (self .m ):
56+ for j in range (self .n ):
57+
58+ if self .board [i ][j ] == 0 :
59+ lives = self .count (i , j )
60+
61+ if lives == 3 :
62+ self .board [i ][j ] = - 1
63+
64+ if self .board [i ][j ] == 1 :
65+ lives = self .count (i , j )
66+
67+ if lives < 2 or lives > 3 :
68+ self .board [i ][j ] = 2
69+
70+ self .update ()
71+
72+
73+
74+
75+ def count (self , row , col ):
76+ result = 0
77+
78+ for d in self .directions :
79+ new_row = row + d [0 ]
80+ new_col = col + d [1 ]
81+
82+ if new_row >= 0 and new_row < self .m and new_col >= 0 and new_col < self .n and (self .board [new_row ][new_col ] == 1 or self .board [new_row ][new_col ] == 2 ):
83+ result + = 1
84+
85+ return result
86+
87+ def update (self ):
88+
89+ for i in range (self .m ):
90+ for j in range (self .n ):
91+
92+ if self .board [i ][j ] == - 1 :
93+ self .board [i ][j ] = 1
94+
95+ if self .board [i ][j ] == 2 :
96+ self .board [i ][j ] = 0
0 commit comments