-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path76.py
37 lines (27 loc) · 883 Bytes
/
76.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
class Solution:
def nextCells(self, cells, n):
newcells = [0] * n
for i in range(1, n - 1):
if cells[i - 1] == cells[i + 1]:
newcells[i] = 1
else:
newcells[i] = 0
return newcells
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
n = len(cells)
i = 0
is_repeated_found = False
visited = set()
while N > 0:
if not is_repeated_found:
curr_state = tuple(self.nextCells(cells, n))
if curr_state in visited:
is_repeated_found = True
N = N % i
else:
visited.add(curr_state)
i += 1
if N > 0:
cells = self.nextCells(cells, n)
N -= 1
return cells