1
1
"""
2
2
289. Game of Life
3
3
4
- Submitted: March 17 , 2025
4
+ Submitted: March 19 , 2025
5
5
6
- Runtime: 2 ms (beats 23.06 %)
7
- Memory: 17.70 MB (beats 94.61 %)
6
+ Runtime: 1 ms (beats 30.59 %)
7
+ Memory: 17.72 MB (beats 76.77 %)
8
8
"""
9
9
10
10
class Solution :
@@ -16,19 +16,15 @@ def gameOfLife(self, board: List[List[int]]) -> None:
16
16
17
17
for i in range (len (board )):
18
18
for j in range (len (board [0 ])):
19
- n = self .sumOfNeighbors (board_copy , i , j )
19
+ n = sum (
20
+ sum (
21
+ board_copy [x ][y ] for y in range (max (0 , j - 1 ), min (len (board_copy [0 ]), j + 2 ))
22
+ if (x , y ) != (i , j )
23
+ )
24
+ for x in range (max (0 , i - 1 ), min (len (board_copy ), i + 2 ))
25
+ )
20
26
if board_copy [i ][j ]:
21
27
if n < 2 : board [i ][j ] = 0
22
28
elif n > 3 : board [i ][j ] = 0
23
29
elif n == 3 :
24
30
board [i ][j ] = 1
25
-
26
-
27
- def sumOfNeighbors (self , board , i , j ):
28
- return sum (
29
- sum (
30
- board [x ][y ] for y in range (max (0 , j - 1 ), min (len (board [0 ]), j + 2 ))
31
- if (x , y ) != (i , j )
32
- )
33
- for x in range (max (0 , i - 1 ), min (len (board ), i + 2 ))
34
- )
0 commit comments