11"""
22289. Game of Life
33
4- Submitted: March 17 , 2025
4+ Submitted: March 19 , 2025
55
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 %)
88"""
99
1010class Solution :
@@ -16,19 +16,15 @@ def gameOfLife(self, board: List[List[int]]) -> None:
1616
1717 for i in range (len (board )):
1818 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+ )
2026 if board_copy [i ][j ]:
2127 if n < 2 : board [i ][j ] = 0
2228 elif n > 3 : board [i ][j ] = 0
2329 elif n == 3 :
2430 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