-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path289.game-of-life.py
64 lines (56 loc) · 1.9 KB
/
289.game-of-life.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# @lc app=leetcode id=289 lang=python3
#
# [289] Game of Life
#
# @lc code=start
# TAGS: Array
class Solution:
# 32 ms, 90%. Time and Space: O(M*N)
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
if not board: return
def count(i, j):
rv = 0
for di in (-1, 0, 1):
for dj in (-1, 0, 1):
if di == dj == 0: continue
if 0 <= i + di < R and 0 <= j + dj < C:
rv += board[i + di][j + dj]
return rv
R, C = len(board), len(board[0])
ans = [[0]*C for _ in range(R)]
for i in range(R):
for j in range(C):
ones = count(i, j)
if board[i][j]:
ans[i][j] = 1 if 2 <= ones <= 3 else 0
elif ones == 3:
ans[i][j] = 1
board[:] = ans
# 32 ms, 90%. Time: O(M*N). Space: O(1)
def gameOfLife(self, board: List[List[int]]) -> None:
if not board: return
def count(i, j):
rv = 0
for di in (-1, 0, 1):
for dj in (-1, 0, 1):
if di == dj == 0: continue
if 0 <= i + di < R and 0 <= j + dj < C:
if board[i + di][j + dj] in (-1, 1, 3):
rv += 1
return rv
R, C = len(board), len(board[0])
for i in range(R):
for j in range(C):
ones = count(i, j)
if board[i][j]:
board[i][j] = 3 if 2 <= ones <= 3 else -1
else:
if ones == 3: board[i][j] = 2
for i in range(R):
for j in range(C):
board[i][j] = 0 if board[i][j] <= 0 else 1
# @lc code=end